PHP Gets the remote Web page content code Fopen,curl measured

Source: Internet
Author: User
1, the use of fopen

Copy the Code code as follows:


$handle = fopen ("http://s.jb51.net/", "RB");
$contents = "";
while (!feof ($handle)) {
$contents. = Fread ($handle, 8192);
}
Fclose ($handle);
Echo $contents; The output gets the content.
?>


Copy the Code code as follows:


The following code can be used for PHP 5 and later versions
$handle = fopen ("Http://s.jb51.net", "RB");
$contents = Stream_get_contents ($handle);
Fclose ($handle);
Echo $contents;
?>


But the above code easily appears failed to open Stream:http request failed! error, workaround
Some people say that in php.ini, there are two options: Allow_url_fopen =on (indicates that a remote file can be opened via a URL), user_agent= "PHP" (indicating which script to access the network, by default there is a ";" to remove.) ) Restart the server.
But some will have this warning message, want to use the perfect solution is one step, but also to set the php.ini inside the user_agent,php default user_agent is PHP, we change it to mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) to simulate the browser is available

User_agent= "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) "
In the work encountered this problem, after the perfect solution, so share to everyone.
2, through Curl to achieve

Copy the Code code as follows:


$url = "Http://s.jb51.net";
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout,10);
$dxycontent = curl_exec ($ch);
Echo $dxycontent;
?>


Under Linux, you can download it using the following code
EXEC ("wget {$url}");
The difference between PHP fetching external resource function Fopen/file_get_contents/curl
Fopen/file_get_contents DNS queries are re-queried each time the request is made, and the DNS information is not cached.
However, Curl automatically caches the DNS information. Requests for Web pages or images under the same domain name require only one DNS query.
This greatly reduces the number of DNS queries.
So curl has a much better performance than fopen/file_get_contents.
Original content of this site, reproduced please indicate the source.

The above describes the PHP get remote Web content code Fopen,curl has been measured, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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