使用PHP Curl上传多个图像 [英] Upload multiple images with PHP Curl
问题描述
我正在尝试使用PHP Curl上传多个图像.我正在使用的API提供了以下示例:
I'm trying to upload more than one image with PHP Curl. The API I'm using gives me the following example:
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.com.example.api+json" \
-F "image=@img1.jpeg;type=image/jpeg" \
-F "image=@img2.jpeg;type=image/jpeg" \
-XPUT 'https://example.com/api/sellers/12/ads/217221/images'
因此,在我的php脚本中,我尝试了此操作:
So in my php script I tried this:
$files = array();
foreach($photos as $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', 'test_name');
$files[] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
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.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);
首先,我从API得到以下响应:
First I got this response from the API:
{
"errors":{
"error":{
"@key":"unsupported-form-element"
}
}
}
,但现在完全没有响应. 如何使用curl上传多个文件?
but now there is no response at all. How do I use curl to upload multiple files?
例如,如果$ files是一个空的JSON,则它完全不给出错误并返回图像(当然是空的).
if $files is an empty JSON for example, it gives no error at all and returns the images (empty ofcourse).
这是我正在使用的API的文档: https://services.mobile.de/manual/new-seller- api.html#_upload_images
This is the documentation of the API I'm using: https://services.mobile.de/manual/new-seller-api.html#_upload_images
我试图构建请求正文并将其发送,但是它什么也没做:
I tried to build the request body and send it, but it doesn't do anything:
$requestBody = '';
$requestBody .= '--vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx\r\n';
$requestBody .= 'Content-Disposition: form-data; name="image"; filename="ferrari.JPG"\r\n';
$requestBody .= 'Content-Type: image/jpeg\r\n';
$requestBody .= 'Content-Transfer-Encoding: binary\r\n';
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
推荐答案
try use assoc array, how it use into documentation CURLFile
$photos = [
'img1' => 'img1.jpg',
'img2' => 'img2.jpg'
]
$files = [];
foreach($photos as $key => $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', $key);
$files[$key] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
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.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);
这篇关于使用PHP Curl上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!