Php uses the fsockopen function to send post and get requests to obtain webpage content

Source: Internet
Author: User
This article mainly introduces how php uses the fsockopen function to send post and get requests to obtain webpage content. It is a typical application of PHP for socket programming. For more information, see

This article mainly introduces how php uses the fsockopen function to send post and get requests to obtain webpage content. It is a typical application of PHP for socket programming. For more information, see

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.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 '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 :";
$ 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.

,

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.