图像上传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);

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

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...