php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
来源:互联网 发布:淘宝首页资源位 编辑:程序博客网 时间:2024/05/01 20:09
最近需要获取别人网站上的音乐数据。用了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(
curl_setopt(
curl_setopt($ch, CURLOPT_REFERER,REFERER);
curl_setopt(
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函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦!
看到的其他文章收藏于此===============================
php fsockopen
方法1: 用file_get_contents 以get方式获取内容
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件(curl, fsockopen ,file_get_contents 几个方法的效率对比)
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- php fsockopen curl file_get_contents
- fsockopen/curl/file_get_contents效率比较
- 经典抓取网络数据方法效率分析(fsockopen/curl/file_get_contents)
- php中 curl, fsockopen ,file_get_contents 三个函数
- php中 curl, fsockopen ,file_get_contents 三个函数
- curl, fsockopen ,file_get_contents
- php curl fsockopen 上传文件
- PHP中读取文件的几个方法
- PHP中读取文件的几个方法
- PHP中读取文件的几个方法
- PHP中读取文件的几个方法
- PHP中读取文件的几个方法
- 1053. Path of Equal Weight (30)
- Asp.net B/S模式下使用Console.Write()
- 字符串和字典的相互转化
- 共享onload事件的使用(javascriptDOM编程艺术学习笔记)
- java内存模型
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- Numpy使用3
- UISegment
- Android bootanimation 制作过程
- 基于BoF的图像分类完整步骤(以ScSPM为例)
- Android开发技巧——定制仿微信图片裁剪控件
- c++试题(1)
- 最短路径问题
- 实现button的监听,3种方式