473,219 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

posting with cURL problem

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

Dec 28 '06 #1
4 8010

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...
Dec 29 '06 #2
zorro wrote:
it doesn't work :
curl_setopt($curl, CURLOPT_POSTFIELDS, array('clown'=>'bozo') );
Benjamin wrote:
CURLOPT_POSTFIELDS only takes a string as it's parameter

I didn't find any specification stating that it takes a string
parameter.
http://www.php.net/manual/en/function.curl-setopt.php

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.

Dec 29 '06 #3
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
directly.

Dec 29 '06 #4

zorro wrote:
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
directly.
thank you for noticing that

Dec 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: machodev | last post by:
Hello Friends, Getting a bit fuzzy on the Authorize.net and the Curl Integration. I have the script using the for the authorize.net Integration with the curl command exec("/usr/local/bin -m...
6
by: Wescotte | last post by:
I'm writing a tiny php app that will log into our bank of america account and retrieve a file containing a list of checks that cleared the previous day. The problem I'm running into is when I...
3
by: Velvet | last post by:
I'm having an intermittent problem with IE 6.0.2900.2180.xpsp_sp2_gdr.050301-1519 installed on Windows XP. The images, style sheets and JavaScript files are not loading to the browser. ...
3
by: =?Utf-8?B?Q2hhcmxlc0E=?= | last post by:
I'm using Win XP pro Visual Studio 2005 with ASP.net 2.0 and C# in Stephen Walther's ASP.net 2.0 unleashed he gets you to do a Submit button example with it's postBackUrl set to a different page...
1
by: csteimonts | last post by:
Hey there, if it would be possible, since you have figured out the login function of logging into yahoo from PHP CURL, could you post that solution? I'm having quite a time figuring that...
6
by: =?Utf-8?B?Q29saW4gQmFrZXI=?= | last post by:
I have a problem with IE crashing when attempting to print a complex web page. I know this newsgroup is for general ASP questions but I can't find an IE6 or general IE discussion group in the...
4
by: adam.waterfield | last post by:
I am just wondering if anyone here can help with a problem that we have here. We have a PHP based website/application that has a user login that is connected to our AD setup. This works fine....
11
by: Flexor | last post by:
I have a php script that runs from command line and makes an https request to paypal, using curl. It works fine if I run it from a web page. It fails if I run it from CLI. The error I get from...
6
by: pkchandra999 | last post by:
My Server OS : Centos 64 bit Apache version is Apache v2.2.9 Php Version : 5.2.5 I am providing feeds to my forum through RSS. The feeds are in html and those feeds will be parsed to bbcode...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.