Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

posting with cURL - postfields array won't work

10 views
Skip to first unread message

zorro

unread,
Dec 27, 2006, 7:00:25 PM12/27/06
to
Hello,

I'm really stumped about this one. The problem below happens when i'm
posting to a particular web site but there's no problem when I post to
my online testing server.

this works for both the web site and my testing server:
curl_setopt($curl, CURLOPT_POSTFIELDS, "clown=bozo" );

this works only for my testing server:
curl_setopt($curl, CURLOPT_POSTFIELDS, array('clown'=>'bozo') );

In this case the response from the web site is empty, no headers,
nothing, and there is no cURL error.


I need to use an array for the postfields because I will eventually be
uploading a file thusly:
array('myfile'=>''@c:/images/clowns/bozo.gif").

I searched the web and all I could find was some people append "\n" to
the posted values, so i tried that but to no avail:
array('clown'=>"bozo\n")


the full code:

$curl = curl_init();

$cookiejar =
$_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF']).'/cookiejar.txt';
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiejar );
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiejar );
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows
NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6');
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, 'http://www.somesite.com/form.htm');
$response = curl_exec($curl);

echo $response;

zorro

unread,
Dec 29, 2006, 3:49:14 PM12/29/06
to
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 didn'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
directly.

0 new messages