PHP gets the code for the content of the Remote Web page (Fopen,curl has been measured) _php instance

Source: Internet
Author: User
Tags curl http request
1, the use of fopen
Copy Code code as follows:

<?php
$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 Code code as follows:

You can use the following code for PHP 5 and later
<?php
$handle = fopen ("Http://s.jb51.net", "RB");
$contents = Stream_get_contents ($handle);
Fclose ($handle);
Echo $contents;
?>

But the code above is apt to appear failed to open Stream:http request failed! error, workaround

Some people say that in php.ini, there are two options: Allow_url_fopen =on (which means you can open a remote file via a URL), user_agent= "PHP" (which means that the script accesses the network by default, and the ";") is removed. ) reboot the server.
But some still will have this warning message, want to use perfect solution still one step, still have to set php.ini inside 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 OK

User_agent= "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.0) "

Encounter this problem in the work, after the perfect solution, so share to everybody.
2, through the curl to achieve
Copy Code code as follows:

<?php
$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;
?>


Linux can be downloaded using the following code
EXEC ("wget {$url}");

The difference of PHP fetching external resource function Fopen/file_get_contents/curl

Fopen/file_get_contents DNS queries for each request, and does not cache DNS information.
However, Curl will automatically cache DNS information. Requests for a Web page or picture under the same domain name require only a single DNS query.
This greatly reduces the number of DNS queries.
So curl performance is much better than fopen/file_get_contents.

Cloud Habitat Community Original content, reproduced please indicate the source.

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.