Upload images via API (PHP cURL)

The API manual gives me the following example for loading images using cURL:

Endpoint

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

Curl Team

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'

Inquiry

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

How can I make this request with PHP cURL?

I tried a lot of things but nothing works. For instance:

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

In response from the API, I get an empty image object. What am I doing wrong?

+4
source share

Source: https://habr.com/ru/post/1662141/


All Articles