File_get_contents is a way to get content from a working document or URL, and it's more stable than curl_get_contents.

Source: Internet
Author: User

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

Related Article

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.