/ Published in: PHP
URL: http://www.apphp.com/index.php?snippet=php-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.
Expand |
Embed | Plain Text
<?php // submit these variables to the server // send a request to specified server $result = do_post_request("http://www.example.com/", $post_data); if($result["status"] == "ok"){ // headers // result of the request }else{ } function do_post_request($url, $data, $referer = ""){ // convert the data array into URL Parameters like a=1&b=2 etc. $data = http_build_query($data); // parse the given URL if($url["scheme"] != "http"){ } // extract host and path from url $host = $url["host"]; $path = $url["path"]; // open a socket connection with port 80, set timeout 40 sec. $result = ""; if($fp){ // send a request headers d\r\n"); // receive result from request }else{ } // close socket connection // split result header from the content // return as structured array: "status" => "ok", "header" => $header, "content" => $content ); } ?>
You need to login to post a comment.
