/ Published in: PHP
                    
                                        
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
                            
                        
                        Copy this code and paste it in your HTML
<?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
echo $result["header"];
// result of the request
echo $result["content"];
}else{
echo "An error occurred: ".$result["error"];
}
function do_post_request($url, $data, $referer = ""){
// convert the data array into URL Parameters like a=1&b=2 etc.
// 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
");
");
");
// receive result from request
}else{
}
// close socket connection
// split result header from the content
", $result, 2);
// return as structured array:
"status" => "ok",
"header" => $header,
"content" => $content
);
}
?>
URL: http://www.apphp.com/index.php?snippet=php-post-request-by-socket-connection
Comments
 Subscribe to comments
                    Subscribe to comments
                
                