HTTP POST request from PHP

Thread Solved

Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

HTTP POST request from PHP

 
0
  #1
Jan 1st, 2009
Here's my predicament. I Have managed to send and retrieve some HTTP requests using the GET method successfully using the following code:
  1. function send_to_host($host,$method,$path='/',$data='',$useragent=0){
  2. // Supply a default method of GET if the one passed was empty
  3. if (empty($method)) {
  4. $method = 'GET';
  5. }
  6. $method = strtoupper($method);
  7. $fp = fsockopen($host, 80) or die("Unable to open socket");
  8.  
  9. fputs($fp, "$method $path HTTP/1.1\r\n");
  10. fputs($fp, "Host: $host\r\n");
  11. fputs($fp, "Content-type: application/x-www-form- urlencoded\r\n");
  12. if ($method == 'POST') fputs($fp, "Content-length: " . strlen($data) . "\r\n");
  13. if ($useragent) fputs($fp, "User-Agent: MSIE\r\n");
  14. fputs($fp, "Connection: close\r\n\r\n");
  15. if ($method == 'POST') fputs($fp, $data);
  16.  
  17. while (!feof($fp))
  18. $buf .= fgets($fp,128);
  19.  
  20. fclose($fp);
  21. return $buf;
  22. }
  23.  
  24. send_to_host('humbot.freewebhostingpro.com','GET','/index.php?a=5','');
  25. //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?
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: HTTP POST request from PHP

 
0
  #2
Jan 1st, 2009
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:
  1. send_to_host('humbot.freewebhostingpro.com','POST','/index.php','a=5');
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: HTTP POST request from PHP

 
0
  #3
Jan 2nd, 2009
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.
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: HTTP POST request from PHP

 
1
  #4
Jan 2nd, 2009
>>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:

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

  1. send_to_host('humbot.freewebhostingpro.com','POST','/index.php','a=5');


On another note, the source of the problem is:
  1. fputs($fp, "Content-type: application/x-www-form- urlencoded\r\n");

there should be NO space between the hyphen and urlencoded. It should be:
  1. fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: HTTP POST request from PHP

 
0
  #5
Jan 8th, 2009
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.
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: HTTP POST request from PHP

 
1
  #6
Jan 8th, 2009
glad to help. Take care.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: icandothat is an unknown quantity at this point 
Solved Threads: 0
icandothat icandothat is offline Offline
Newbie Poster

Re: HTTP POST request from PHP

 
0
  #7
Feb 11th, 2009
nice Job on that solution. i was putzing around with this same piece of code and didn't see that either, thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: HTTP POST request from PHP

 
0
  #8
Feb 16th, 2009
>>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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: HTTP POST request from PHP

 
0
  #9
Feb 17th, 2009
Sorry, way to long ago to remember or find out.
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: HTTP POST request from PHP

 
0
  #10
Feb 19th, 2009
humbug, thanks for the reply. Hopefully icandothat can shed some light on the source of pertaining code. Take care.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC