探讨file_get_contents与curl效率及稳定性的分析

编程学习 2021-07-05 09:49www.dzhlxh.cn编程入门
本篇文章是对file_get_contents与curl效率及稳定性进行了详细的分析介绍,需要的朋友参考下

做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效:

代码如下:

$config['context'] = stream_context_create(array(‘http' => array(‘method' => “GET”,
   'timeout' => 5//这个超时时间不稳定,经常不奏效
   )
  ));

这时候,看一下服务器的连接池,会发现一堆类似的错误,让你头疼万分:
file_get_contents(http://***): failed to open stream…
不得已,安装了curl库,写了一个函数替换:
代码如下:

<span style="color:#000000; font-weight:bold">function</span> curl_file_get_contents<span style="color:#009900">(</span><span style="color:#000088">$durl</span><span style="color:#009900">)</span><span style="color:#009900">{</span>
&nbsp;&nbsp;&nbsp;<span style="color:#000088">$ch</span> <span style="color:#339933">=</span> <span style="color:#990000">curl_init</span><span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_URL<span style="color:#339933">,</span> <span style="color:#000088">$durl</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_TIMEOUT<span style="color:#339933">,</span> <span style="color:#cc66cc">5</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_USERAGENT<span style="color:#339933">,</span> _USERAGENT_<span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_REFERER<span style="color:#339933">,</span>_REFERER_<span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_RETURNTRANSFER<span style="color:#339933">,</span> <span style="color:#cc66cc">1</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#000088">$r</span> <span style="color:#339933">=</span> <span style="color:#990000">curl_exec</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#990000">curl_close</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
&nbsp;&nbsp;&nbsp;<span style="color:#b1b100">return</span> <span style="color:#000088">$r</span><span style="color:#339933">;</span>
&nbsp;<span style="color:#009900">}</span>

如此,除了真正的网络问题外,没再出现任何问题。
这是别人做过的关于curl和file_get_contents的测试:
file_get_contents抓取google.com需用秒数:
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
curl使用的时间:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
差距很大吧?呵呵,从我使用的经验来说,这两个工具不只是速度有差异,稳定性也相差很大。建议对网络数据抓取稳定性要求比较高的朋友使用上面的curl_file_get_contents函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦!

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板