Php uses the fsockopen function to send a post, get request to obtain webpage content, and fsockopen webpage content _ PHP Tutorial

Source: Internet
Author: User
Php uses the fsockopen function to send a post, get request to obtain webpage content, and fsockopen webpage content. Php uses the fsockopen function to send post and get requests to obtain webpage content. the fsockopen webpage content example in this article describes how php uses the fsockopen function to send post, php that uses the fsockopen function to send a post request to get webpage content. the get request is used to obtain 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:

The 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:

The 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:

The 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.

Examples in this article describes how php uses the fsockopen function to send a post and get request to get the webpage content...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.