curl not sending post field's to my page.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
FounderSim
Forum Newbie
Posts: 8
Joined: Fri Feb 10, 2012 4:50 pm

curl not sending post field's to my page.

Post 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?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post by twinedev »

All the years I have used cURL, never had tried that, thanks!
FounderSim
Forum Newbie
Posts: 8
Joined: Fri Feb 10, 2012 4:50 pm

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

Post 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
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post 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" ....
FounderSim
Forum Newbie
Posts: 8
Joined: Fri Feb 10, 2012 4:50 pm

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

Post 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
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post 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?
Post Reply