By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

PHP post without CURL

P: 69
Hi,

I'm using the following code to perform a post request from PHP. It works fine on a server running PHP 5, but not on a server running PHP 4. I've tried switching error reporting on but it's not giving any output.

Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL);
  2. ini_set('display_errors', true);
  3.  
  4. //CALL FUNCTION, AND PRINT OUT SERVERS RESPONSE
  5. echo do_post_request('http://www.example.com/form.php?form=7',
  6.     'CustomFields[7]=Sean&email=sean@example.com');
  7.  
  8. //should work in PHP 4 and 5
  9. function do_post_request($url, $data, $optional_headers = null) {
  10.    $start = strpos($url,'//')+2;
  11.    $end = strpos($url,'/',$start);
  12.    $host = substr($url, $start, $end-$start);
  13.    $domain = substr($url,$end);
  14.    $fp = pfsockopen($host, 80, $erstr, $errno, 5);
  15.    if(!$fp) return null;
  16.    fputs ($fp,"POST $domain HTTP/1.1\n");
  17.    fputs ($fp,"Host: $host\n");
  18.    if ($optional_headers) {
  19.       fputs($fp, $optional_headers);
  20.    }
  21.    fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
  22.    fputs ($fp,"Content-length: ".strlen($data)."\n\n");
  23.    fputs ($fp,"$data\n\n");
  24.    $response = "";
  25.    while(!feof($fp)) {
  26.       $response .= fgets($fp, 1024);
  27.    }
  28.    fclose ($fp);
  29.    return $response;
  30. }
  31.  
Any ideas?

Thanks,

Sean
Oct 27 '08 #1
Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Does the code seem to *stall* anywhere, or is it simply getting nothing from the request?
Try using var_dump on the return value and see what it gives you.
You could also add a few echo statements along the way to see exactly what is going on.

Also, and this may not actually matter, but typically a line in a HTTP request is ended by "\r\n", rather than just "\n". Although any decent HTTP server should parse it regardless.

Another thing you could try is adding the "Connection: close" header.
Not passing it can sometimes lead to delays, which might well exceed your PHP script's max execution time.

O, and also. Are there any major differences between the two servers, besides the PHP version?
Oct 27 '08 #2

P: 69
I've put in echo statements and var_dump and I'm not getting any output without commenting out all the post code. I've tried different code and I'm still not getting any output. Not sure why.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /*
  4. ** The function:
  5. */
  6.  
  7. function PostRequest($url, $referer, $_data) {
  8.  
  9.     // convert variables array to string:
  10.     $data = array();    
  11.     while(list($n,$v) = each($_data)){
  12.         $data[] = "$n=$v";
  13.     }    
  14.     $data = implode('&', $data);
  15.     // format --> test1=a&test2=b etc.
  16.  
  17.     // parse the given URL
  18.     $url = parse_url($url);
  19.     if ($url['scheme'] != 'http') { 
  20.         die('Only HTTP request are supported !');
  21.     }
  22.  
  23.     // extract host and path:
  24.     $host = $url['host'];
  25.     $path = $url['path'];
  26.  
  27.      echo "Host is: $host<br />";
  28.  
  29.     // open a socket connection on port 80
  30.     $fp = fsockopen($host, 80);
  31.  
  32.     // send the request headers:
  33.     fputs($fp, "POST $path HTTP/1.1\r\n");
  34.     fputs($fp, "Host: $host\r\n");
  35.     fputs($fp, "Referer: $referer\r\n");
  36.     fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  37.     fputs($fp, "Content-length: ". strlen($data) ."\r\n");
  38.     fputs($fp, "Connection: close\r\n\r\n");
  39.     fputs($fp, $data);
  40.  
  41.     $result = ''; 
  42.     while(!feof($fp)) {
  43.         // receive the results of the request
  44.         $result .= fgets($fp, 128);
  45.     }
  46.  
  47.     // close the socket connection:
  48.     fclose($fp);
  49.  
  50.     // split the result header from the content
  51.     $result = explode("\r\n\r\n", $result, 2);
  52.  
  53.     $header = isset($result[0]) ? $result[0] : '';
  54.     $content = isset($result[1]) ? $result[1] : '';
  55.  
  56.     // return as array:
  57.     return array($header, $content);
  58. }
  59.  
  60.  
  61.  
  62. /*
  63. ** The example:
  64. */
  65.  
  66. // submit these variables to the server:
  67. $data = array(
  68.     'CustomFields[7]' => 'Sean',
  69.     'email' => 'sean@example.com'
  70. );
  71.  
  72. // send a request to example.com (referer = jonasjohn.de)
  73. list($header, $content) = PostRequest(
  74.     "http://www.example.com/form.php?form=7",
  75.     $data
  76. );
  77.  
  78. // print the result of the whole request:
  79. print $content;
  80.  
  81. // print $header; --> prints the headers
  82.  
  83. ?>
  84.  
Hi.

Does the code seem to *stall* anywhere, or is it simply getting nothing from the request?
Try using var_dump on the return value and see what it gives you.
You could also add a few echo statements along the way to see exactly what is going on.

Also, and this may not actually matter, but typically a line in a HTTP request is ended by "\r\n", rather than just "\n". Although any decent HTTP server should parse it regardless.

Another thing you could try is adding the "Connection: close" header.
Not passing it can sometimes lead to delays, which might well exceed your PHP script's max execution time.

O, and also. Are there any major differences between the two servers, besides the PHP version?
Oct 27 '08 #3

dlite922
Expert 100+
P: 1,584
Did you make sure error_reporting() and display errors is On?
Oct 28 '08 #4

P: 69
Hi,

No sorry. I had them in my previous attempt. Just added the following to the top of the script.

Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL);
  2. ini_set( 'display_errors', '1' );
  3. echo "Hello";
  4.  
And it's not even printing the hello. This script is available at: http://www.theresolutionsplanner.co.uk/newtest.php.

Thanks,

Sean
Oct 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.