I’m trying to post via curl, I’ve been using the same code over and over again with no problem but now I need to be able to use an array for posts (i’m not sure if there’s a proper term for that?).
I should clarify that it’s specifically a file i’m trying to post, but I can’t get it working with a string either so I don’t think it’s too do with that.
This is absouletly fine:
$uploadData = array(); $uploadData['uploads'] = "@".$file; $uploadData['iagree'] = 'on';
This doesn’t appear to work:
$uploadData = array(); $uploadData['uploads'][0] = "@".$file; $uploadData['iagree'] = 'on';
In the second example i’m trying to replicate an input with the attribute name=”uploads[]”
Obviously i’m trying to curl an external site, but if I experiment curling a page on my own server so that I can see what’s being sent, I can see that the uploads array is being converted to a string:
print_r($_POST); print_r($_FILES);
returns:
Array
(
[uploads] => Array
[iagree] => on
)
Array
(
)
This is my full Curl:
$uploadData = array();
$uploadData['uploads'][] = "@".$file;
$uploadData['iagree'] = 'on';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $theLink);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $uploadData);
$upload_response = curl_exec($ch);
curl_close($ch);
I’ve tried to give as much information as possible, but if i’ve missed something feel free to ask and i’ll provide more.
Other than that, does anyone have any suggestions or solutions?
$uploadData['uploads[]'] = "@".$file;and passing it as an array should work, just keep in mind you need the absolute path to the file.There is no mechanism in ‘simple’ HTTP (multipart/form-data or application/x-www-form-urlencoded) to send ‘arrays’. However, PHP interprets the
[&]characters in key-value pairs as special. PHP is alone in that AFAIK, it’s not a HTTP mechanism, it’s just the parsing of input PHP does, as is replacing.‘s in the name of values with_. Curl is a 3rd party package which lives seperately from PHP, and as such does not understand multidimensional arrays.