0

I'm writing a script to uploading images to a specific api. Uploading one image is not a problem but more than one. The API documentation says the following:

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'

My script:

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

The problem is that the api delete other images if I'm uploading a new one. But I can't write an array with multiple same keys like that:

$images = array(
    'image' => new CURLFile('PATH\1.jpg', 'image/jpeg', 'image'),
    'image' => new CURLFile('PATH\2.jpg', 'image/jpeg', 'image')
);

So the question is how I can set multiple CURLFiles with the same index like the curl command in the documentation?

8
  • See this post stackoverflow.com/questions/44474192/…, May it will help you. Commented Aug 1, 2017 at 11:30
  • No, unfortunately not because the api respond: {"errors":[{"key":"unsupported-form-element"}]} That means that the key of the cURL-File MUST be "image" and nothing other... but thank you very much! Commented Aug 1, 2017 at 11:42
  • then try your Curl call in looping. Commented Aug 1, 2017 at 11:45
  • We talking about services.mobile.de/docs/seller-api.html ? Their documentation sure does have multiple "image" named multipart form datas with different file names... how odd Commented Aug 1, 2017 at 11:48
  • 1
    No i cant because the api says: {"errors":[{"key":"unsupported-form-element"}]} It must be an array with "image" as key... Commented Aug 1, 2017 at 11:52

2 Answers 2

0

Working at mobile seller api ;-) Here's the solution: 1. create an array of image-files like this one:

$count=0;
$files['image'.$count] = curl_file_create(#a#, 'image/jpeg', #b#)
$count++;

-> #a# full qualified path to your file on your server -> #b# optionally a name for the image in the form 'image'.$count

Important: the array-key MUST be namend exactly in this way while you can name your array as you want ;-)

  1. Options for curl: - remove the "host" from your HTTP_HEADER and add CURLOPT_CONNECTTIMEOUT with a value in seconds ;-)

By the way, in all actual php versions, we use TRUE and FALSE instead of 0 and 1 as values for curl-options.

It works for me ...

Sign up to request clarification or add additional context in comments.

Comments

-1

My experience after hours of searching: With the official curl function, you can not make an multiple-upload of images.

You can only upload multiple images using the shell command. Solution: Extend the shell script to include all images to be uploaded, then "shell_exec ($ str)" or exec ($ str). For me this works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.