I'm having trouble sending a multidimensional array with file uploads using PHP and CURL.
A multidimensional array, for example:
$post['question'] = 'Are you human?'; $post['answers'] = array('yes', 'no', 'maybe'); $post['file'] = '@/path/to/file'; // Output: Array( 'question' => Are you human?, 'answers' => Array( '0' => yes, '1' => no, '2' => maybe ), 'file' => @/path/to/file )
There are several reasons why this will not work if you simply try to publish this using CURLOPT_POSTFIELDS in CURL as follows:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $response = curl_exec($ch);
First of all, the official PHP description of CURLOPT_POSTFIELDS says:
Full details for publishing to HTTP "POST" operation. To publish the file, add filename with @ and use the full path. This can be passed as urlencoded string like 'para1 = val1 & para2 = val2 & ...' or as an array with the field name as the key and field data as the value. If the value is an array, the Content-Type header will set to multipart / form-data.
It looks like you can pass any type of POSTFIELDS array correctly? Wrong. POSTFIELDS accepts only non-scalar values ββand suppresses the Array to string conversion error when transmitting multidimensional arrays. So the only other option you have is http_build_query() your array in order to be able to pass multidimensional arrays that don't choke.
But .. as you can read in the note on the PHP page:
Note. Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart / form-data, while passing a URL encoded string will encode the data as using / x-www-form-urlencoded.
The message will not be encoded in multipart / form-data if you pass the urlencoded line to POSTFIELDS, which will cause the file to fail to load.
Thus, it seems almost impossible to combine the two with CURL, while that would not be a problem if you were to use a regular HTML form.
My question is: can I get around this weird CURL quirk to be able to send multidimensional arrays and upload files?