在PHP文件的上传的RESTful [英] RESTful uploading of a file in PHP

查看:118
本文介绍了在PHP文件的上传的RESTful的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我工作的那将会通过RESTful接口上传到服务器的视频脚本。文档告诉我,我应该通过数据(包括二进制视频文件)作为POST请求的一部分。我知道如何设置我的POST变量,但我不知道该怎么办二进制数据。该API说我应该有一个叫做媒体领域,它应该包含原始视频数据。

So I'm working on a script that's going to upload a video to a server via a RESTful interface. The documentation tells me that I should pass the data (including the binary video file) as part of a POST request. I know how to set my POST variables, but I'm not sure how to do the binary data. The API says I should have a field called 'media' and it should contain the raw video data.

所以我们可以说我有一个名为video1.mp4的视频,我想在我的'媒体'POST变量的内容。我怎样才能做到这一点?

So let's say I have a video called 'video1.mp4' and I want to include its contents in my 'media' POST variable. How can I do this?

谢谢!

推荐答案

我不知道你是如何与API交流,但我会承担卷曲的这个例子。要发送的文件,您可以使用 CURLOPT_POSTFIELDS 选项:

I don't know how you're communication with the API, but I'll assume cURL for this example. To send files, you use the CURLOPT_POSTFIELDS option:

CURLOPT_POSTFIELDS 结果
  完整的数据张贴在HTTPPOST操作。要发布一个文件,prePEND用@文件名,并使用完整路径。或与字段名一样价值的关键和现场数据的数组这可以作为一个urlen codeD串像; PARA2 = val2的&放... PARA1 = VAL1和放大器'的形式传递。如果值是一个数组,在Content-Type头将被设置为multipart / form-数据。

CURLOPT_POSTFIELDS
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

通过一个例子进一步下跌的网页上:

With an example further down on the page:

$ch = curl_init();

$data = array('name' => 'Foo', 'media' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);

这篇关于在PHP文件的上传的RESTful的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆