图像上传CURL命令到PHP Curl

前端之家收集整理的这篇文章主要介绍了图像上传CURL命令到PHP Curl前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 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”param无效.有人可以请帮助

$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,CURLOPT_POSTFIELDS,$data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);

var_dump($data);

解决方法

尽量给出完整的图像路径而不是’@ / path / to / images / milk.jpg’;

这是更新的代码

$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';


$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/milk.jpg';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/peas.jpg';


$curl = curl_init($imgUrl);
curl_setopt($curl,CURLINFO_HTTP_CODE);
curl_close($curl);

var_dump($data);

如果这不起作用,请告诉我.

原文链接:https://www.f2er.com/linux/394861.html

猜你在找的Linux相关文章