PHP uses the Fsockopen function to send Post,get request a way to get the content of the Web page, fsockopen the content _php tutorial

Source: Internet
Author: User

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

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