1

I need to send a POST request via PHP cURL. It needs to include both a file and an array parameter. The raw curl command should look like this:

curl "https://the.url.com"                                                                                                            
  -F file="@/path/to/file.xml"
  -F 'list_item[]=foo'
  -F 'list_item[]=bar'

Unfortunately the list_item[] parameter cannot have keys (i.e. list_item[0], list_item[1], etc...) otherwise the server throws an error.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://the.url.com');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
         'file' => curl_file_create($file),
         'list_item' => [
             'foo',
             'bar'
         ]
    ]);
    $response = curl_exec($ch);

This must be producing the wrong curl command because it's also being rejected.

Can anyone help?

Robin Fuller
  • 145
  • 2
  • 8
  • I guess you need to use `http_build_query` - look at [this](https://stackoverflow.com/questions/3772096/posting-multidimensional-array-with-php-and-curl) – dWinder Feb 27 '19 at 15:16
  • @dWinder it seems to break the file parameter. The server thinks I am not uploading a file. – Robin Fuller Feb 27 '19 at 15:42
  • Could you use a session variable? – Perelx Mar 13 '19 at 21:07
  • @Perelx I don't own the server/app I'm posting to. In the end, the owner allowed me to use query string parameters. So it's not longer a problem for me. – Robin Fuller Mar 15 '19 at 09:23

0 Answers0