Upload multiple images using PHP Curl

I am trying to upload more than one image using PHP Curl. The API I use 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' 

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

First I got a response from the API:

  { "errors":{ "error":{ "@key":"unsupported-form-element" } } } 

but now there is no answer. How to use curl to upload multiple files?

if $ files is an empty JSON, for example, it does not give any error and returns images (empty).

This is the API documentation I'm using: https://services.mobile.de/manual/new-seller-api.html#_upload_images

EDIT: I tried to create a request body and send it, but it does nothing:

 $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); 
+5
source share
1 answer

try using the helper array as used in the CURLFile documentation

 $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); 
+1
source

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


All Articles