PHP fopen/file_get_contents与curl机能比拼
1.fopen/file_get_contents 每次恳求都会从新做 DNS 查询,并不合错误 DNS 信息停止缓存。
但是 CURL 会主动对 DNS 信息停止缓存。对统一域名下的网页或者图片的恳求只需要一次 DNS 查询。这大大减少了 DNS 查询的次数。所以 CURL 的机能比 fopen /file_get_contents 好许多。
2.fopen/file_get_contents 在恳求 HTTP 时,使用的是 http_fopen_wrapper,不会 keeplive。
而 curl 却可以。这样在屡次恳求多个链接时,curl 效力会好一些。
3.fopen/file_get_contents 函数会受到 php.ini 文件中 allow_url_open 选项配置的影响。
假如该配置关闭了,则该函数也就失效了。而 curl 不受该配置的影响。
4.curl 可以模拟多种恳求,例如:POST 数据,表单提交等,会员可以依照本人的需求来定制恳求。
而 fopen /file_get_contents 只能使用 get 方式猎取数据。
file_get_contents 猎取长途文件时会把结果都存在一个字符串中 fiels 函数则会贮存成数组情势
因此,我还是比力倾向于使用 curl 来拜访长途 url。Php 有 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 函数,不单不乱速度快,还能假冒阅读器诈骗目标地址哦
再看一个实例
后续贴出了 curl 和 file_get_contents 的对照结果,这边除了 curl 与 file_get_contents 的机能对照,还包括了他们的机能对照,讲此前看下如下的结果图:
curl 与 file_get_contents 机能对照 PHP 源代码如下:
<?php /** * 通过淘宝IP接口猎取IP地理位置 * @param string $ip * @return: string **/ function getCityCurl($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); $ipinfo=json_decode($file_contents); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } function getCity($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ipinfo=json_decode(file_get_contents($url)); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } // for file_get_contents $startTime=explode(' ',microtime()); $startTime=$startTime[0] + $startTime[1]; for($i=1;$i<=10;$i++) { echo getCity("121.207.247.202")."</br>"; } $endTime = explode(' ',microtime()); $endTime = $endTime[0] + $endTime[1]; $totalTime = $endTime - $startTime; echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds</br>"; //for curl $startTime2=explode(' ',microtime()); $startTime2=$startTime2[0] + $startTime2[1]; for($i=1;$i<=10;$i++) { echo getCityCurl('121.207.247.202')."</br>"; } $endTime2 = explode(' ',microtime()); $endTime2=$endTime2[0] + $endTime2[1]; $totalTime2 = $endTime2 - $startTime2; echo "curl:".number_format($totalTime2, 10, '.', "")." seconds"; ?>
file_get_contents 速度:4.2404510975 seconds
curl 速度:2.8205530643 seconds
curl 比 file_get_contents 速度快了 30% 摆布,最重要的是效劳器负载更低.
总结
file_get_contents 处置频繁小的时候,用它感受挺好的。没什么非常。假如你的文件被 1k + 人处置。那么你的效劳器 cpu 就等着高升吧。所以倡议本人和大家在今后写 php 代码的时候使用 curl 库。
以上就是PHP fopen/file_get_contents与curl机能比力的具体内容,更多请关注百分百源码网其它相关文章!