HTTP POST request from PHP
Here's my predicament. I Have managed to send and retrieve some HTTP requests using the GET method successfully using the following code:
function send_to_host($host,$method,$path='/',$data='',$useragent=0){
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
$fp = fsockopen($host, 80) or die("Unable to open socket");
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form- urlencoded\r\n");
if ($method == 'POST') fputs($fp, "Content-length: " . strlen($data) . "\r\n");
if ($useragent) fputs($fp, "User-Agent: MSIE\r\n");
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') fputs($fp, $data);
while (!feof($fp))
$buf .= fgets($fp,128);
fclose($fp);
return $buf;
}
send_to_host('humbot.freewebhostingpro.com','GET','/index.php?a=5','');
//sent from localhost
I have tried a few times to send some data via POST but haven't had any luck. (I haven't had any trace of the POST data getting through)
(set up a form to submit some GET and POST data to humbot.freewebhostingpro.com/index.php and the page will display that data)
Any ideas?
humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
you are passing only three parameters. For post, your third parameter should be only the php script that processes the request(without the querystring parameters) and the fourth parameter should be the "querystring" data. Try:
send_to_host('humbot.freewebhostingpro.com','POST','/index.php','a=5');
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
Notice how line 24 of my code includes "GET" and not "POST"? That's the call I used to (successfully) send a GET request (as the English above clearly states).
I have slightly modified the code making up that function from the code I got off the net so I understand what needs to go in which parameter.
I have tried the line you suggested an a few similar (even with the original code) but none have shown me that my POST data was received.
humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
>>Notice how line 24 of my code includes "GET" and not "POST"? That's the call I used to (successfully) send a GET request (as the English above clearly states).
Yes, I can see that, but it is NOT clear if you were doing this:
send_to_host('humbot.freewebhostingpro.com','POST','/index.php?a=5','');
which is NOT the correct way to call it. The parameters should not be appended to the uri. Instead you need:
send_to_host('humbot.freewebhostingpro.com','POST','/index.php','a=5');
On another note, the source of the problem is:
fputs($fp, "Content-type: application/x-www-form- urlencoded\r\n");
there should be NO space between the hyphen and urlencoded. It should be:
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
Thanks for that last bit of info, it has actually made my requests successful. You've been a big help because HTTP headers is something I have only briefly read over.
Cheers.
humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
>>i was putzing around with this same piece of code and didn't see that either
out of curiosity, where did you guys get that code from?
>>thanks.
you are welcome.
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
Sorry, way to long ago to remember or find out.
humbug
Junior Poster in Training
93 posts since Oct 2005
Reputation Points: 20
Solved Threads: 13
humbug, thanks for the reply. Hopefully icandothat can shed some light on the source of pertaining code. Take care.
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244