RESTful 在 PHP 中上传文件 [英] RESTful uploading of a file in PHP
问题描述
所以我正在编写一个脚本,该脚本将通过 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 通信的,但我会假设这个例子是 cURL.要发送文件,您可以使用 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"操作中发布的完整数据.要发布文件,请在文件名前加上 @ 并使用完整路径.这可以作为 urlencoded 字符串(如 'para1=val1¶2=val2&...')或作为以字段名称作为键和字段数据作为值的数组传递.如果 value 是数组,则 Content-Type 标头将设置为 multipart/form-data.
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¶2=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);
这篇关于RESTful 在 PHP 中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!