加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php中file_get_contents与curl性能比较分析

发布时间:2020-12-13 02:08:41 所属栏目:PHP教程 来源:网络整理
导读:《PHP实例:php中file_get_contents与curl性能比较分析》要点: 本文介绍了PHP实例:php中file_get_contents与curl性能比较分析,希望对您有用。如果有疑问,可以联系我们。 PHP实战 本篇章节讲解php中file_get_contents与curl性能比较分析.分享给大家供大

《PHP实例:php中file_get_contents与curl性能比较分析》要点:
本文介绍了PHP实例:php中file_get_contents与curl性能比较分析,希望对您有用。如果有疑问,可以联系我们。

PHP实战本篇章节讲解php中file_get_contents与curl性能比较分析.分享给大家供大家参考.具体如下:

PHP实战在php中如果不仔细的去分析性能会发现file_get_contents与curl两个同很多共同点的,他们都可以采集文件打开文件,但是如果仔细一对比会发现很多不同点,下面我们一起来看看file_get_contents与curl区别.

PHP实战PHP中fopen,file_get_contents,curl函数的区别:

PHP实战1.fopen /file_get_contents 每次哀求都会重新做DNS查询,并不对 DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的哀求只需要一次DNS查询.这大大减少了DNS查询的次数.所以CURL的性能比fopen /file_get_contents 好很多.

PHP实战2.fopen /file_get_contents 在哀求HTTP时,使用的是http_fopen_wrapper,不会keeplive.而curl却可以.这样在多次哀求多个链接时,curl效率会好一些.

PHP实战3.fopen / file_get_contents 函数会受到php.ini文件中allow_url_open选项配置的影响.如果该配置关闭了,则该函数也就失效了.而curl不受该配置的影响.

PHP实战4.curl 可以模拟多种哀求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制哀求.而fopen / file_get_contents只能使用get方式获取数据.
file_get_contents 获取远程文件时会把结果都存在一个字符串中 fiels函数则会储存成数组形式

PHP实战因此,我还是比较倾向于使用curl来拜访远程url.Php有curl模块扩展,功能很是强大.

PHP实战说了半天大家可能说性能怎么没对比呢,那我们就来看看

PHP实战最近需要获取别人网站上的音乐数据.用了file_get_contents函数,但是总是会遇到获取失败的问题,尽管依照手册中的 例子设置了超时,可多数时候不会奏效:

代码如下:
$config['context'] = stream_context_create(array('http' => array('method' => "GET",
?? 'timeout' => 5//这个超时时间不稳定,经常不奏效
?? )
));

这时候,看一下服务器的连接池,会发现一堆类似的错误,让我头疼万分:

PHP实战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

PHP实战差距很大?呵呵,从我使用的经验来说,这两个工具不只是速度有差异,稳定性也相差很大.

PHP实战建议对网络数据抓取稳定性要求比较高的朋友使用上面的 curl_file_get_contents函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦

PHP实战再看一个实例

PHP实战后续贴出了curl和file_get_contents的对比结果,这边除了curl与file_get_contents的性能对比,还包含了他们的性能对比,讲之前看下如下的结果图:

PHP实战PHP实例:php中file_get_contents与curl性能比较分析

PHP实战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,$url);
??? curl_setopt ($ch,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,"")." seconds";
?>

测试拜访

file_get_contents速度:4.2404510975 seconds
curl速度:2.8205530643 seconds
curl比file_get_contents速度快了30%左右,最重要的是服务器负载更低.

PHP实战总结

PHP实战file_get_contents处理频繁小的时候,用它感觉挺好的.没什么异常.如果你的文件被1k+人处理.那么你的服务器cpu就等着高升吧.所以建议自己和大家在以后写php代码的时候使用curl库.

PHP实战希望本文所述对大家的PHP程序设计有所赞助.

欢迎参与《PHP实例:php中file_get_contents与curl性能比较分析》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读