我正在编写一个脚本来上传图片到一个特定的api。上传一张图片不是问题,但不止一个。该API文档说以下内容:PHP - cURL文件上传
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.de.xxx.api+json" \
-F "[email protected];type=image/jpeg" \
-F "[email protected];type=image/jpeg" \
-XPUT 'https://services.xxx.de/seller-api/sellers/123/ads/123/images'
我的脚本:
$ch = curl_init();
$images = array(
'image' => new CURLFile('PATH\1.jpg', 'image/jpeg', 'image')
);
curl_setopt($ch, CURLOPT_URL, 'https://services.xxx.de/seller-api/sellers/123/ads/123/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $images);
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.xxx.de',
'Content-Type: multipart/form-data',
'Accept: application/vnd.de.xxx.api+json'
));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
的问题是,API删除其它影像,如果我上传一个新的。但我不能写有多个相同的密钥像一个数组:
$images = array(
'image' => new CURLFile('PATH\1.jpg', 'image/jpeg', 'image'),
'image' => new CURLFile('PATH\2.jpg', 'image/jpeg', 'image')
);
所以,问题是我怎么可以设置多个CURLFiles与像文档中的curl命令相同的索引?
看到这个职位https://stackoverflow.com/questions/44474192/upload-multiple-files-with-curl,或许它会帮助你。 –
不,不幸的是,不是因为api回应:{“errors”:[{“key”:“unsupported-form-element”}]}这意味着cURL文件的关键字必须是“image”,而不是其他。 ..但非常感谢你! –
然后在循环中尝试您的Curl调用。 –