PHP fsockopen Collection of Web content instances _php Tutorial

Source: Internet
Author: User
Fsockopen is a more useful function in PHP, the following I will introduce the use of Fsockopen function to collect Web pages of the program, there is a need for friends to refer to.

Usage
int Fsockopen (string hostname, int port, int [errno], string [errstr], int [timeout]);

A collection of web instances

The code is as follows Copy Code
< p>!--? php
function get_url ($url, $cookie =false)
{
$url = Parse_url ($url);
$query = $url [path]. "?". $url [query];
echo "Query:". $query;
$fp = Fsockopen ($url [host], $url [port]? $url [port]:80, $errno, $errstr, 30);
if (! $fp) {
return false;
} else {
$request = "GET $query http/1.1rn";
$request. = "Host: $url [Host]rn";
$request. = "Connection:closern";
if ($cookie) $request. = "Cookie: $cookien";
$request. = "RN";
Fwrite ($fp, $request);
while (! @feof ($fp)) {
$result. = @fgets ($fp, 1024x768);
}
Fclose ($FP);
return $result;
}
}
//Get the HTML portion of the URL, remove the header
function geturlhtml ($url, $cookie =false)
{
$rowdata = Get_url ($url, $cookie);
if ($rowdata)
{
$body = stristr ($rowdata, "rnrn");
$body =substr ($body, 4,strlen ($body));
return $body;
}

return false;
}
?

The workaround after being disabled

The server also disables Fsockopen Pfsockopen, which is replaced by other functions, such as stream_socket_client (). Note: The parameters for Stream_socket_client () and Fsockopen () are different.

Fsockopen (Replace with Stream_socket_client (, then, the port parameter "80" in the original Fsockopen function is deleted and added to the $host.

Cases

The code is as follows Copy Code

$fp = Fsockopen ($host, $errno, $errstr, 30);
Or
$fp = Fsockopen ($host, $port, $errno, $errstr, $connection _timeout);
After modification:
$fp = Stream_socket_client ("tcp://". $host. " ", $errno, $errstr, 30);
Or
$fp = Stream_socket_client ("tcp://". $host. ":". $port, $errno, $errstr, $connection _timeout);


http://www.bkjia.com/PHPjc/444623.html www.bkjia.com true http://www.bkjia.com/PHPjc/444623.html techarticle Fsockopen is a more useful function in PHP, the following I will introduce the use of Fsockopen function to collect Web pages of the program, there is a need for friends to refer to. Usage int fsockopen (string hostn ...

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