Page 1 of 1

curl not sending post field's to my page.

Posted: Mon Feb 18, 2013 1:05 pm
by FounderSim
I am trying to make part of my script send some data to another page. It doesn't seem to be working correctly. It connects to the URL it is suppose to, but it doesn't post the fields.

My code:

Code: Select all

$postvars='';
				$sep='';
				foreach($fields as $key=>$value) 
				{ 
				   $postvars.= $sep.urlencode($key).'='.urlencode($value); 
				   $sep='&'; 
				}

				$ch = curl_init();
				curl_setopt($ch,CURLOPT_URL, $row['site_postback']);
				curl_setopt($ch,CURLOPT_POST,count($fields));
				curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
				$result = curl_exec($ch);
				curl_close($ch);[/php]
Any idea's how to post the fields?

Re: curl not sending post field's to my page.

Posted: Mon Feb 18, 2013 3:11 pm
by Eric!
Like I tell everyone who ever has a curl problem: The first step when working with curl is to use it in the debug mode.

Code: Select all

$fp=fopen("debug.txt","w");
// init curl here then, configure to redirect the output to a file
curl($ch,CURLOPT_STDERR,$fp);
curl($ch,CURLOPT_VERBOSE,TRUE);
// do your curl stuff stuff here and close it
fclose($fp);
echo "<pre>".file_get_contents("debug.txt")."</pre>";
Also you shouldn't need to urlencode your keys, but I don't think it will hurt anything. If curl is getting redirected it probably isn't reposting your data. There's a number of other things that can effect what happens to post data too depending on the type of connection you're trying to establish. So if you can't find the cause of your problem, repost the debug.txt here.

Re: curl not sending post field's to my page.

Posted: Mon Feb 18, 2013 4:30 pm
by twinedev
All the years I have used cURL, never had tried that, thanks!

Re: curl not sending post field's to my page.

Posted: Mon Feb 18, 2013 10:02 pm
by FounderSim
here is my debug.txt file
* About to connect() to www.textgamemaker.com port 80 (#0)
* Trying 50.22.195.155...
* connected
* Connected to www.textgamemaker.com (50.22.195.155) port 80 (#0)
> POST /postbacktestphp HTTP/1.1

Host: www.textgamemaker.com

Accept: */*

Content-Length: 47

Content-Type: application/x-www-form-urlencoded



* upload completely sent off: 47 out of 47 bytes
< HTTP/1.1 404 Not Found

< Date: Tue, 19 Feb 2013 03:56:13 GMT

< Server: Apache

< Last-Modified: Wed, 04 Apr 2012 05:00:34 GMT

< Accept-Ranges: bytes

< Content-Length: 3354

< Content-Type: text/html

<

* Connection #0 to host www.textgamemaker.com left intact
* Closing connection #0

Re: curl not sending post field's to my page.

Posted: Mon Feb 18, 2013 11:23 pm
by Eric!
@twindev cURL is pretty complex; it's practically a language on it's own. In fact the php extensions can't do a lot of things the current builds of curl support. And it seems I learn something new about it whenever I dig deeper. But I must admit I feel like a broken record on this forum repeating how to get the debug info over and over....

@FounderSim this should be a clue:[text]< HTTP/1.1 404 Not Found[/text]
The page you're trying to post to:

Code: Select all

> POST /postbacktestphp HTTP/1.1
Doesn't exist. Is it supposed to be "postbacktest.php" and not "postbacktestphp" ....

Re: curl not sending post field's to my page.

Posted: Tue Feb 19, 2013 2:13 pm
by FounderSim
curl must be taking the . out because it is reaching my postbacktest.php page.

the problem wasn't within that script.

it was in the postbacktest.php page. I was using $_GET's instead of $_POST's

Re: curl not sending post field's to my page.

Posted: Tue Feb 19, 2013 3:34 pm
by Eric!
From what you posted curl is definitely not reaching your test page. There's no reason curl would remove the dot either. Are you sure the only problem is the data method?