views:

44

answers:

2

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?

A: 

Try passing the query string:

 $uploadData = 'uploads[]=@' . $file . '&iagree=on&uploads[]=@' . $file2;

See if that works for you.

EDIT

Reading through the manual, the string needs to be urlencoded, try this:

$uploadData = urlencode('uploads[]=@' . $file . '&iagree=on&uploads[]=@' . $file2);

Received this information from the curl_setopt() man 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.

I may have used the urlencode improperly, try this:

$uploadData = 'uploads[]=' . urlencode('@' . $file) . '&iagree=' . urlencode('on') . '&uploads[]=' . urlencode('@' . $file2);

UPDATE

Ok this is my last shot at it. Reading through some user comments at the curl page I found something about serializing the sub-array. So:

$uploadData = array('iagree' => 'on', 'uploads' => serialize(array('@' . $file)));

Hopefully that is the key. If that does not work...well it may not be possible to do.

Give that a shot and see if it works. (Sorry for the trial and error, I do not have a method to test it!)

Brad F Jacobs
That sends the string "@root/to/my/testfile.zip" rather than the actual file. But it does send the $_POST as an array.
Rob
Updated with information according the man page at http://www.php.net/curl_setopt given that the string needs to be urlencoded to be passed with the right encoding.
Brad F Jacobs
url encoding it is even worse, absouletly nothing is posted. I've used curl many times without urlencoding so i'm not sure if it is necessary. Nonetheless, it doesn't help in this instance, cheers though!
Rob
Ok, updated again, maybe that will work, not sure. Sorry for the trials and errors.
Brad F Jacobs
This has the same response as my first comment. Posts are now sent as an array as required, but not as files it's just the string "@root/to/my/testfile.zip"
Rob
Ok, final try. Hopefully it works! If not, I highly doubt it is possible to do.
Brad F Jacobs
Thanks for all the help but no that does not work either that just sends the serialization as a string: `a:1:{i:0;s:53:"@root/to/my/testfile.zip";}`It seems a ridiculous oversite if it isn't possible, but thanks for the help nonetheless!
Rob
Oh one more thing although i'm sure you would have found it if you knew. Is there a specific term for inputs with array based names? It would help with my Google Searching
Rob
A: 

$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.

Wrikken
Thankyou that works perfectly, sorry for the late reply. It was the middle of the night when I first posted this question :D
Rob