Doing POST Request by Socket Connection

This example of code shows how to do a simple POST request in PHP to another web server by using a socket connection.

[View All Snippets]
Show Plain Text »
  1. <?php          
  2. // submit these variables to the server
  3. $post_data = array("test"=>"yes", "passed"=>"yes", "id"=>"3");
  4.  
  5. // send a request to specified server
  6. $result = do_post_request("http://www.example.com/", $post_data);
  7. if($result["status"] == "ok"){
  8.     // headers
  9.     echo $result["header"];
  10.     // result of the request
  11.     echo $result["content"];
  12. }else{
  13.     echo "An error occurred: ".$result["error"];
  14. }
  15.  
  16. function do_post_request($url, $data, $referer = ""){
  17.     // convert the data array into URL Parameters like a=1&b=2 etc.
  18.     $data = http_build_query($data);
  19.     // parse the given URL
  20.     $url = parse_url($url);
  21.    
  22.     if($url["scheme"] != "http"){
  23.         die("Error: only HTTP requests supported!");
  24.     }
  25.  
  26.     // extract host and path from url
  27.     $host = $url["host"];
  28.     $path = $url["path"];
  29.  
  30.     // open a socket connection with port 80, set timeout 40 sec.
  31.     $fp = fsockopen($host, 80, $errno, $errstr, 40);
  32.     $result = "";
  33.  
  34.     if($fp){
  35.         // send a request headers
  36.         fputs($fp, "POST $path HTTP/1.1\r\n");
  37.         fputs($fp, "Host: $host\r\n");
  38.         if($referer != "") fputs($fp, "Referer: $referer\r\n");
  39.         fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  40.         fputs($fp, "Content-length: ".strlen($data)."\r\n");
  41.         fputs($fp, "Connection: close\r\n\r\n");
  42.         fputs($fp, $data);
  43.        
  44.         // receive result from request
  45.         while(!feof($fp)) $result .= fgets($fp, 128);
  46.     }else{
  47.         return array("status"=>"err", "error"=>"$errstr ($errno)");
  48.     }
  49.  
  50.     // close socket connection
  51.     fclose($fp);
  52.  
  53.     // split result header from the content
  54.     $result = explode("\r\n\r\n", $result, 2);
  55.  
  56.     $header = isset($result[0]) ? $result[0] : "";
  57.     $content = isset($result[1]) ? $result[1] : "";
  58.  
  59.     // return as structured array:
  60.     return array(
  61.         "status" => "ok",
  62.         "header" => $header,
  63.         "content" => $content
  64.     );
  65. }
  66.  
  67. ?>
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.