Php uses the fsockopen function to send a post, get request to obtain webpage content, and fsockopen webpage content
This example describes how php uses the fsockopen function to send a post request and get a webpage content. Share it with you 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 'Access successful ';
}
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")])
Start a socket to connect to the resources of the specified host. php supports the targets in the Internet field and the description of the socket transmission list supported by unix, the supported transmission list can also be retrieved using stream_get_transports ().
This socket is enabled by default in blocking mode. You can switch to non-blocking mode and use stream_set_blocking (). If the above instance does not understand it, let's take a look. The Code is as follows:
Copy codeThe Code is as follows: $ fp = fsockopen ("www.jb51.net", 80, $ 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 PHP programming.