Re: posting with cURL problem




zorro wrote:
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') );
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;
}


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: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; // this echos nothing and when saved to a file the
file is 0 bytes...

.



Relevant Pages

  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)
  • RE: Structure conversion from C++ to VB-2008?
    ... One of the most important structures is AmiVar structure. ... point number, the array of floating point numbers, a string or IDispatch ... Dim 13012679 as Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Structure conversion from C++ to VB-2008?
    ... One of the most important structures is AmiVar structure. ... point number, the array of floating point numbers, a string or IDispatch ... Dim 13012679 as Integer ...
    (microsoft.public.dotnet.languages.vb)