Curl_get_contents is more stable than file_get_contents.
Source: Internet
Author: User
I believe all the friends who have used the file_get_contents function know that when the obtained $ url cannot be accessed, it will lead to a long wait for the page, and even cause the PHP process to occupy 100% of the CPU, therefore, this function is born to share a function actually in use:
The code is as follows:
/* More stable than file_get_contents! $ Timeout is the timeout time in seconds. the default value is 1 s. */
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.bitsCN.com ');
I believe all the friends who have used the file_get_contents function know that when the obtained $ url cannot be accessed, it will lead to a long wait for the page, and even cause the PHP process to occupy 100% of the CPU, so this function was born. Curl introduction
The reason for retaining the original file_get_contents function is that it is more appropriate to use native file_get_contents when reading local files.
Another from the banquet file_get_contnets optimization, see: http://www.bitsCN.com/article/28030.htm
Set the timeout time to solve this problem. If curl is not installed, you must use this method.
The code is as follows:
$ Ctx = stream_context_create (array (
'Http' => array (
'Timeout' => 1 // set a timeout time, in seconds
)
)
);
File_get_contents ("http://www.bitsCN.com/", 0, $ ctx );
In addition, due to incomplete tests, the use of curl to retrieve pages is much more stable than file_get_contents.
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