PHP uses the Fsockopen function to send Post,get request to obtain the content of the Web page _php tips

Source: Internet
Author: User

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.

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.