PHP post without CURL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebarry
    New Member
    • Jul 2007
    • 69

    PHP post without CURL

    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.

    Code:
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    
    //CALL FUNCTION, AND PRINT OUT SERVERS RESPONSE
    echo do_post_request('http://www.example.com/form.php?form=7',
    	'CustomFields[7]=Sean&email=sean@example.com');
    
    //should work in PHP 4 and 5
    function do_post_request($url, $data, $optional_headers = null) {
       $start = strpos($url,'//')+2;
       $end = strpos($url,'/',$start);
       $host = substr($url, $start, $end-$start);
       $domain = substr($url,$end);
       $fp = pfsockopen($host, 80, $erstr, $errno, 5);
       if(!$fp) return null;
       fputs ($fp,"POST $domain HTTP/1.1\n");
       fputs ($fp,"Host: $host\n");
       if ($optional_headers) {
          fputs($fp, $optional_headers);
       }
       fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
       fputs ($fp,"Content-length: ".strlen($data)."\n\n");
       fputs ($fp,"$data\n\n");
       $response = "";
       while(!feof($fp)) {
          $response .= fgets($fp, 1024);
       }
       fclose ($fp);
       return $response;
    }
    Any ideas?

    Thanks,

    Sean
    Last edited by Atli; Oct 27 '08, 09:12 PM. Reason: Replaced real URL with example.com
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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?

    Comment

    • Sebarry
      New Member
      • Jul 2007
      • 69

      #3
      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.

      Code:
      <?php
      
      /*
      ** The function:
      */
       
      function PostRequest($url, $referer, $_data) {
       
          // convert variables array to string:
          $data = array();    
          while(list($n,$v) = each($_data)){
              $data[] = "$n=$v";
          }    
          $data = implode('&', $data);
          // format --> test1=a&test2=b etc.
       
          // parse the given URL
          $url = parse_url($url);
          if ($url['scheme'] != 'http') { 
              die('Only HTTP request are supported !');
          }
       
          // extract host and path:
          $host = $url['host'];
          $path = $url['path'];
       
       	echo "Host is: $host<br />";
      	
          // open a socket connection on port 80
          $fp = fsockopen($host, 80);
       
          // send the request headers:
          fputs($fp, "POST $path HTTP/1.1\r\n");
          fputs($fp, "Host: $host\r\n");
          fputs($fp, "Referer: $referer\r\n");
          fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
          fputs($fp, "Content-length: ". strlen($data) ."\r\n");
          fputs($fp, "Connection: close\r\n\r\n");
          fputs($fp, $data);
       
          $result = ''; 
          while(!feof($fp)) {
              // receive the results of the request
              $result .= fgets($fp, 128);
          }
       
          // close the socket connection:
          fclose($fp);
       
          // split the result header from the content
          $result = explode("\r\n\r\n", $result, 2);
       
          $header = isset($result[0]) ? $result[0] : '';
          $content = isset($result[1]) ? $result[1] : '';
       
          // return as array:
          return array($header, $content);
      }
       
       
       
      /*
      ** The example:
      */
      
      // submit these variables to the server:
      $data = array(
          'CustomFields[7]' => 'Sean',
          'email' => 'sean@example.com'
      );
       
      // send a request to example.com (referer = jonasjohn.de)
      list($header, $content) = PostRequest(
          "http://www.example.com/form.php?form=7",
          $data
      );
       
      // print the result of the whole request:
      print $content;
       
      // print $header; --> prints the headers
      
      ?>
      Originally posted by Atli
      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?
      Last edited by Atli; Oct 28 '08, 06:52 PM. Reason: Replaced real URL and email with example values.

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        Did you make sure error_reporting () and display errors is On?

        Comment

        • Sebarry
          New Member
          • Jul 2007
          • 69

          #5
          Hi,

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

          Code:
          error_reporting(E_ALL);
          ini_set( 'display_errors', '1' );
          echo "Hello";
          And it's not even printing the hello. This script is available at: http://www.theresoluti onsplanner.co.u k/newtest.php.

          Thanks,

          Sean

          Comment

          Working...