php 简评file_get_contents与curl 效率及稳定性
2014-03-16 22:49
489 查看
做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效:
$config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,
’timeout’ => 5//这个超时时间不稳定,经常不奏效
)
));
这时候,看一下服务器的连接池,会发现一堆类似的错误,让你头疼万分:
file_get_contents(http://***): failed to open stream…
不得已,安装了curl库,写了一个函数替换:
如此,除了真正的网络问题外,没再出现任何问题。
这是别人做过的关于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函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦
$config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,
’timeout’ => 5//这个超时时间不稳定,经常不奏效
)
));
这时候,看一下服务器的连接池,会发现一堆类似的错误,让你头疼万分:
file_get_contents(http://***): failed to open stream…
不得已,安装了curl库,写了一个函数替换:
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; } |
这是别人做过的关于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函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦
相关文章推荐
- 简评file_get_contents与curl 效率及稳定性
- 简评file_get_contents与curl 效率及稳定性
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- 关于测试curl 与file_get_contents抓取内容效率及稳定性
- 探讨file_get_contents与curl效率及稳定性的分析
- PHP中file_get_contents于curl性能效率比较
- 【转】file_get_contents与curl效率及稳定性问题
- php读取网络文件(curl, fsockopen ,file_get_contents 几个方法的效率对比)
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- file_get_contents与curl效率及稳定性问题
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- 探讨file_get_contents与curl效率及稳定性的分析
- file_get_contents与curl效率及稳定性问题
- php中file_get_content 和curl以及fopen 效率分析
- php远程抓取函数curl(),file_get_contents
- php中 curl, fsockopen ,file_get_contents 三个函数
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- php中file_get_contents()与curl()性能比较