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.