通过API上传图片(PHP cURL)

时间:2016-11-28 13:29:24

标签: php api curl

API手册给出了以下使用cURL上传图像的示例:

端点

  

PUT / seller-api / sellers /:mobileSellerId / ads /:mobileAdId / images

卷曲命令

curl -v -s -u username:password \
  -H "Content-Type: multipart/form-data" \
  -H "Accept: application/vnd.de.mobile.api+json" \
  -F "image=@img1.jpeg;type=image/jpeg" \
  -F "image=@img2.jpeg;type=image/jpeg" \
  -XPUT 'https://services.mobile.de/seller-api/sellers/12/ads/217221/images'

请求

PUT /seller-api/sellers/12/ads/217221/images HTTP/1.1
Host: services.mobile.de
Content-Type: multipart/form-data; boundary=vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx

--vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx
Content-Disposition: form-data; name="image"; filename="img1.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

如何使用PHP cURL发出此请求?

我尝试过很多东西,但似乎没什么用。 例如:

$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';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.mobile.de/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.mobile.de: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.mobile.de',
    'Content-type: multipart/form-data; boundary="vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx"',
    'Accept: application/vnd.de.mobile.api+json'
));
$output = curl_exec($ch);

curl_close($ch);

return $output;

作为API的响应,我得到一个空图像对象。 我做错了什么?

0 个答案:

没有答案