且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PHP文件的上传的RESTful

更新时间:2023-11-30 13:42:58

我不知道你是如何与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);