This article illustrates how PHP implements a method that imitates the return page of a socket request. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
<?php
$url = "www.XXXX.com"; Make your own replacements.
$parse = Parse_url ($url); Resolves the URL and returns the component.
$host = $parse [' Host '];
$path = $parse [' path '];
$port = 80;
$timeout = 80;
$fp = @fsockopen ($host, $port, $errno, $errstr, $timeout); Open the Socket link
if (! $fp) {
echo $errno. " -". $errstr; If error, return error code and error message
} else {
$out = "POST $path http/1.1\r\n"; The following is the HTTP request header information
$out. = "Host:". $host. " \ r \ n ";
$out. = "Accept: */*\r\n";
$out. = "connection:close\r\n";
$out. = "Cookie: $cookie \r\n\r\n";
@fwrite ($fp, $out); Write the request information to the link
$status = Stream_get_meta_data ($FP);
if (! $status [' timed_out ']) {
while (!feof ($fp)) {
if ($header = @fgets ($fp)) && ($header = = "\ r \ n" | | $header = = "\ n")) {
Break
}
}
$stop = false;
while (!feof ($FP) &&! $stop) {
$data = Fread ($fp, 8192); 8192 is the number of bytes that can be returned
$return. = $data;
}
}
Fclose ($FP);
Print_r ($return);
}
I hope this article will help you with your PHP program design.