温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP中fopen,file_get_contents,curl函数有什么区别

发布时间:2020-11-12 14:16:02 来源:亿速云 阅读:166 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关PHP中fopen,file_get_contents,curl函数有什么区别的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

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函数有什么区别就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI