PHP fopen/file_get_contents与curl性能比较-php教程

资源魔 34 0
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 抓取 谷歌.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功能比拟的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!