【发布时间】:2016-03-31 07:40:25
【问题描述】:
我对 PHP CURL 很陌生,并试图调用 API 来进行图像上传(一次多个文件)。 API 文档在 CURL 中给出了以下示例。我已经测试过了,它正在工作。
curl -H "Authorization: Bearer 123456789" -i -X POST -F "whitespace=1" \
-F "imageData[]=@/path/to/images/milk.jpg" \
-F "imageData[]=@/path/to/images/peas.jpg" \
https://mytestapi.com/1.0/uploadimages
现在我需要将其转换为 PHP Curl。出于某种原因,我总是收到错误“imageData”参数无效。有人可以帮忙吗
$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';
$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData[]'] = '@/path/to/images/milk.jpg';
$data_to_post['imageData[]'] = '@/path/to/images/peas.jpg';
$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($data);
【问题讨论】:
-
你会给谁赏金
-
感谢赏金。如果你愿意,你可以投票
标签: php curl libcurl image-upload