posting with cURL problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello there,

I can't figure out why is it that when i use an array for my postfields
it doesn't work :

this works
curl_setopt($curl, CURLOPT_POSTFIELDS, "clown=bozo" );

this doesn't
curl_setopt($curl, CURLOPT_POSTFIELDS, array('clown'=>'bozo') );

The web site i post to only responds when my parameters are in a
string,  but it sends nothing back when i use an array - no headers, no
error, nothing. And cURL shows no error either. So there must be
something different in the way cURL posts from an array. It's like the
web site reads it differently than when i post a string and possibly
some error is generated on the server. I make other posts to the same
web site and all is okay except when i use an array. Any ideas what
might be going on?

Here is the full code

$curl = curl_init();

curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookiejar.txt' );
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookiejar.txt' );
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows
NT 5.1; en-US; rv: Gecko/20060728 Firefox/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('clown'=>'bozo') );

curl_setopt($curl, CURLOPT_URL, ' ');
$response = curl_exec($curl);

echo $response;  // this echos nothing and when saved to a file the
file is 0 bytes...

Re: posting with cURL problem

zorro wrote:
Quoted text here. Click to load it
If you look at the php manual (http://www.php/curl_setop ), you'll
notice that the option CURLOPT_POSTFIELDS only takes a string as it's
parameter. PHP doesn't convert the array into the proper POST fields by
itself. You have to make a function for this your self.
Try this:
function array_to_post($array) {
$post = "";
foreach ($array as $name => $value) {
$post .= "&$name=$value";
return $post;
Quoted text here. Click to load it

Re: posting with cURL problem

Quoted text here. Click to load it

Benjamin wrote:
Quoted text here. Click to load it

I didn't find any specification stating that it takes a string

There are dozens of examples where a string or an array is used and i
have also tried both successfully. The problem is only on this one
particular web site I'm posting to.

I need to use an array and not a string because for posting a file
upload one needs to use this syntax:
curl_setopt($curl, CURLOPT_POSTFIELDS,
array('mypic'=>"@path/to/image.gif") );

I tested that this works on my localhost and online testing server.

Re: posting with cURL problem

Ok I found the reason, hope this helps someone eventually:

You can see headers sent by cURL by using

$mydebug = fopen('debug.txt','w');
curl_setopt($curl, CURLOPT_STDERR, $mydebug);
curl_setopt($curl, CURLOPT_VERBOSE, 1);

I noticed those headers were different when posting with an array.
Namely, there is a "Expect: 100-continue header" which basically tells
the server that some content will be posted but only if the server
responds back with "HTTP/1.1 100 Continue" code. Why on the web site I
was posting to the continuing doesn't happen automatically like on my
testing servers i don't know. I tried setting headers like "Connection:
keep-alive" but it didn't help. What did work though was removing the
"Expect" header :

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));

So now cURL doesn't ask permission to post first but just posts

Re: posting with cURL problem

zorro wrote:
Quoted text here. Click to load it
thank you for noticing that

Site Timeline