I'm having problems with posting a multidimensional array with file uploads using PHP and CURL.
The multidimensional array is 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 a few things why this wouldn't work if you simply try to post this with CURLOPT_POSTFIELDS in CURL like this:
$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:
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
It sounds like you can pass any kind of array to POSTFIELDS right? Wrong. POSTFIELDS only accepts non-scalar values and will choke with a Array to string conversion
error when passing on multi-dimensional arrays. So, the only other option you have is to http_build_query()
your array 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 application/x-www-form-urlencoded.
The post will not be multipart/form-data encoded if you pass an urlencoded string to POSTFIELDS, causing your file upload to fail.
So it seems nearly impossible to combine the two with CURL, while it wouldn't be a problem if you'd used a regular HTML form.
My question is: is it possible to bypass this weird quirk of CURL to be able to post multidimensional arrays and file uploads?