This example describes how PHP uses the Fsockopen function to send post,get requests to get web content. Share to everyone for your reference.
The specific implementation code is as follows:
Copy Code code as follows:
$post = 1;
$url = Parse_url ($url);
$host = ' http://www.jb51.net ';
$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 visit ';
}
Fsockopen Syntax:
Copy Code code as follows:
Resource Fsockopen (string $hostname [, int $port =-1 [, int & $errno [, String & $errstr [, Float $timeout = Ini_get ( "Default_socket_timeout")]]]
To start a socket connection to a specified host of resources, PHP supports the target in the Internet domain and UNIX in the supported socket transfer list description, the supported transport list can also be retrieved using Stream_get_transports ().
The socket preset will be enabled, blocking mode, you can switch to non-blocking mode using stream_set_blocking (), if the above example does not understand, look at a Jane, the code is as follows:
Copy Code 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 will help you with your PHP program design.