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.