Home > Backend Development > PHP Tutorial > Discuss the analysis of file_get_contents and curl efficiency and stability_PHP tutorial

Discuss the analysis of file_get_contents and curl efficiency and stability_PHP tutorial

WBOY
Release: 2016-07-21 15:08:24
Original
991 people have browsed it

I have worked on many products that capture content from other websites, and I am used to using the convenient and fast file_get_contents function, but I always encounter the problem of failure to obtain it. Even though I set the timeout according to the examples in the manual, it does not work most of the time. :

Copy code The code is as follows:

$config['context'] = stream_context_create(array('http' => ; array('method' => "GET",
'timeout' => 5//This timeout is unstable and often does not work
)
));

At this time, if you look at the server’s connection pool, you will find a bunch of similar errors, which will give you a headache:
file_get_contents(http://***): failed to open stream…
As a last resort, I installed the curl library and wrote a function replacement:
Copy the code The code is as follows:

function curl_file_get_contents($durl){
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $durl);
   curl_setopt($ch, CURLOPT_TIMEOUT, 5);
   curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
   curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $r = curl_exec($ch);
   curl_close($ch);
   return $r;
 }

So no more problems other than real network issues.
This is a test done by others about curl and file_get_contents:
The number of seconds it takes for file_get_contents to crawl google.com:
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
Time used by curl:
0.68719101
0.64675593
0.64326
0.81983113
0.63 956594
Is there a big gap? Haha, from my experience, these two tools are not only different in speed, but also in stability. It is recommended that friends who have high requirements for the stability of network data capture use the curl_file_get_contents function above. Not only is it stable and fast, it can also fake the browser and spoof the target address!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327449.htmlTechArticleI have made many products that capture the content of other websites. I am used to using the convenient and fast file_get_contents function, but I always get it. When encountering the problem of failure to obtain, despite following the examples in the manual...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template