It is believed that friends who have used the file_get_contents function know that when the acquired access to the. PHP is not accessible, it can cause the page to wait a long time, and even cause the process to take up 100% CPU, so this function is born to share a function that is actually in use:
Copy CodeThe code is as follows:
/* More stable than file_get_contents! The $timeout is the time-out, in seconds, and the default is 1s. */
function curl_get_contents ($url, $timeout =1) {
$curlHandle = Curl_init ();
curl_setopt ($curlHandle, Curlopt_url, $url);
curl_setopt ($curlHandle, Curlopt_returntransfer, 1);
curl_setopt ($curlHandle, Curlopt_timeout, $timeout);
$result = curl_exec ($curlHandle);
Curl_close ($curlHandle);
return $result;
}
$HX = curl_get_contents (' http://www.jb51.net ');
It is believed that friends who have used the file_get_contents function know that when the acquired access to the. PHP is not accessible, it can cause the page to wait a long time, and even cause the process to take up 100% CPU, so this function is born. Curl a few common sense introduction
The reason for preserving the original file_get_contents function is that it is obviously more appropriate to use native file_get_contents when reading local files.
Another from the Zhang Yi of the File_get_contnets optimization, specific to see: http://www.jb51.net/article/28030.htm
Also set a timeout to resolve this issue. If you don't have curl, you have to use this method.
Copy CodeThe code is as follows:
$ctx = stream_context_create (Array (
' http ' = = Array (
' Timeout ' = 1//Set a time-out in seconds
)
)
);
File_get_contents ("http://www.jb51.net/", 0, $ctx);
In addition, it is not completely tested, using curl to get the page more stable than with file_get_contents.
File_get_contents is a way to get content from a working file or URL, and it's more stable than curl_get_contents