PHP uses the Fsockopen function to send a post,get request to get the content of the Web page, Fsockopen Web content
This example describes how PHP uses the Fsockopen function to send post,get requests for Web content. Share to everyone for your reference.
The specific implementation code is as follows:
Copy CodeThe code is as follows: $post = 1;
$url = Parse_url ($url);
$host = ' http://www.bkjia.com ';
$path = '/';
$query = '? action=phpfensi.com ';
$port = 80;
if ($post) {
$out = "Post $path http/1.0";
$out. = "Accept: */*";
$out. = "Referer: $boardurl";
$out. = "ACCEPT-LANGUAGE:ZH-CN";
$out. = "content-type:application/x-www-form-urlencoded";
$out. = "User-agent: $_server[http_user_agent]";
$out. = "Host: $host";
$out. = ' content-length: '. strlen ($post). " ";
$out. = "Connection:close";
$out. = "Cache-control:no-cache";
$out. = "Cookie: $cookie";
$out. = $post;
} else {
$out = "Get $path http/1.0";
$out. = "Accept: */*";
$out. = "Referer: $boardurl";
$out. = "ACCEPT-LANGUAGE:ZH-CN";
$out. = "User-agent: $_server[http_user_agent]";
$out. = "Host: $host";
$out. = "Connection:close";
$out. = "Cookie: $cookie";
}
$fp = @fsockopen (($ip? $ip: $host), $port, $errno, $errstr, $timeout);
if (! $fp)
{
Return ";//note $errstr: $errno
} else {
Return ' successful access ';
}
Fsockopen Syntax:Copy CodeThe code is as follows: Resource Fsockopen (string $hostname [, int $port =-1 [, int & $errno [, String & $errstr [, float $timeout = ini _get ("Default_socket_timeout")]]
To initiate a socket connection to the specified host resource, PHP supports the target in the Internet domain and UNIX supports the socket transmission list description, and the supported transport list can also be retrieved using Stream_get_transports ().
The socket presets will be enabled, blocking mode, you can switch to non-blocking mode using stream_set_blocking (), if the above example does not understand, see a Jane bar, the code is as follows:
Copy the code as follows: $fp = Fsockopen ("Www.jb51.net", $errno, $errstr, 30);
if (! $fp) {
echo "$errstr ($errno)";
} else {
$out = "get/http/1.1";
$out. = "Host:www.jb51.net";
$out. = "Connection:close";
Fwrite ($fp, $out);
while (!feof ($fp)) {
Echo fgets ($FP, 128);
}
Fclose ($FP);
}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/912282.html www.bkjia.com true http://www.bkjia.com/PHPjc/912282.html techarticle PHP uses the Fsockopen function to send Post,get request to obtain the content of the Web page method, fsockopen Web page Content This article describes the PHP use Fsockopen function send Post,get request to obtain the content of the Web page ...