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 模块扩展,功能很是强大。

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

  1. #最近需要获取别人网站上的音乐数据。用了file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的 例子设置了超时,可多数时候不会奏效:
  2.  
  3. $config['context'] = stream_context_create(array(‘http =>array(‘method => GET”,
  4.  
  5. timeout => 5//这个超时时间不稳定,经常不奏效
  6.  
  7. )
  8.  
  9. ));
  10.  
  11. #这时候,看一下服务器的连接池,会发现一堆类似的错误,让我头疼万分:
  12.  
  13. file_get_contents(http://***): failed to open stream…
  14.  
  15. #现在改用了curl库,写了一个函数替换:
  16.  
  17. function curl_file_get_contents($durl){
  18.  
  19. $ch = curl_init();
  20.  
  21. curl_setopt($ch, CURLOPT_URL, $durl);
  22.  
  23. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  24.  
  25. curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
  26.  
  27. curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
  28.  
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  30.  
  31. $r = curl_exec($ch);
  32.  
  33. curl_close($ch);
  34.  
  35. return $r;
  36.  
  37. }

  

如此,除了真正的网络问题外,没再出现任何问题。

这是别人做过的关于 curl 和 file_get_contents 的测试:

file_get_contents 抓取 google.com 需用秒数:

  1. 2.31319094
  2.  
  3. 2.30374217
  4.  
  5. 2.21512604
  6.  
  7. 3.30553889
  8.  
  9. 2.30124092

curl 使用的时间:

  1. 0.68719101
  2.  
  3. 0.64675593
  4.  
  5. 0.64326
  6.  
  7. 0.81983113
  8.  
  9. 0.63956594

差距很大?

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

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

再看一个实例

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

curl 与 file_get_contents 性能对比 PHP 源代码如下:

  1. <?php
  2.  
  3. /**
  4.  
  5. * 通过淘宝IP接口获取IP地理位置
  6.  
  7. * @param string $ip
  8.  
  9. * @return: string
  10.  
  11. **/
  12.  
  13. function getCityCurl($ip)
  14.  
  15. {
  16.  
  17. $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
  18.  
  19. $ch = curl_init();
  20.  
  21. $timeout = 5;
  22.  
  23. curl_setopt ($ch, CURLOPT_URL, $url);
  24.  
  25. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  26.  
  27. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  28.  
  29. $file_contents = curl_exec($ch);
  30.  
  31. curl_close($ch);
  32.  
  33. $ipinfo=json_decode($file_contents);
  34.  
  35. if($ipinfo->code=='1'){
  36.  
  37. return false;
  38.  
  39. }
  40.  
  41. $city = $ipinfo->data->region.$ipinfo->data->city;
  42.  
  43. return $city;
  44.  
  45. }
  46.  
  47. function getCity($ip)
  48.  
  49. {
  50.  
  51. $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
  52.  
  53. $ipinfo=json_decode(file_get_contents($url));
  54.  
  55. if($ipinfo->code=='1'){
  56.  
  57. return false;
  58.  
  59. }
  60.  
  61. $city = $ipinfo->data->region.$ipinfo->data->city;
  62.  
  63. return $city;
  64.  
  65. }
  66.  
  67. // for file_get_contents
  68.  
  69. $startTime=explode(' ',microtime());
  70.  
  71. $startTime=$startTime[0] + $startTime[1];
  72.  
  73. for($i=1;$i<=10;$i++)
  74.  
  75. {
  76.  
  77. echo getCity("121.207.247.202")."</br>";
  78.  
  79. }
  80.  
  81. $endTime = explode(' ',microtime());
  82.  
  83. $endTime = $endTime[0] + $endTime[1];
  84.  
  85. $totalTime = $endTime - $startTime;
  86.  
  87. echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds</br>";
  88.  
  89. //for curl
  90.  
  91. $startTime2=explode(' ',microtime());
  92.  
  93. $startTime2=$startTime2[0] + $startTime2[1];
  94.  
  95. for($i=1;$i<=10;$i++)
  96.  
  97. {
  98.  
  99. echo getCityCurl('121.207.247.202')."</br>";
  100.  
  101. }
  102.  
  103. $endTime2 = explode(' ',microtime());
  104.  
  105. $endTime2=$endTime2[0] + $endTime2[1];
  106.  
  107. $totalTime2 = $endTime2 - $startTime2;
  108.  
  109. echo "curl:".number_format($totalTime2, 10, '.', "")." seconds";
  110.  
  111. ?>

  

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

  

总结

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

PHP fopen/file_get_contents与curl性能比较的更多相关文章

  1. php file_get_contents与curl性能比较

    1.fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的请求只需要一次DNS ...

  2. PHP file_get_contents于curl性能效率比较

    说明大部分内容整理来源于网络,期待你的补充.及不当之处的纠正: 1)fopen/file_get_contents 每次请求远程URL中的数据都会重新做DNS查询,并不对DNS信息进行缓存.但是CUR ...

  3. 微信公众号 openId 支付 php中file_get_contents与curl性能比较分析

    w http://www.jb51.net/article/57238.htm

  4. PHP中fopen,file_get_contents,curl函数的区别

    PHP中fopen,file_get_contents,curl函数的区别 1.fopen/file_get_contents每次请求都做DNS查询,并不对DNS的信息进行缓存,而curl会对DNS的 ...

  5. [转]PHP中fopen,file_get_contents,curl的区别

    1.       fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的请求只 ...

  6. fopen,file_get_contents,curl的区别

    1.       fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的请求只需 ...

  7. nginx+fastcgi php 使用file_get_contents、curl、fopen读取localhost本站点.php异常的情况

    原文:http://www.oicto.com/nginx_fastcgi_php_file_get_contents/ 参考:http://os.51cto.com/art/201408/44920 ...

  8. PHP file_get_contents和curl区别

    一.file_get_contents 1.定义 file_get_contents() 函数将指定 URL 的文件读入一个字符串并返回. 2.语法 file_get_contents(path, i ...

  9. PHP为fopen,file_get_contents等函数请求web地址时增加Http头的方法

    我们在使用fsockopen时可以方便的自定义自己请求的http头内容来访问某些对客户端请求头有特殊限制的网站,但是使用fopen,file_get_contents等函数请求web地址时怎么来灵活定 ...

随机推荐

  1. Mac下多版本JDK安装及管理

    在Java项目中,经常对JDK版本有不同的要求,可是不可能为了某个项目的运行重新下载不同版本JDK进行安装,这样就涉及到对本地环境中多个JDK版本的管理. Mac的JDK都是安装到一个指定目录的:/L ...

  2. centos 7.6 配置VNC

    一.安装 1.  以root用户运行以下命令来安装vncserver; yum install tigervnc-server 2.  同样运行以下命令来安装vncviewer; yum instal ...

  3. 【OF框架】框架规范介绍

    一.目录规范 二.命名规范 三.其它规范

  4. Sql Server 2017 安装问题记录

    记录了我在虚拟机中安装Sql server 2017遇到的一些问题. 安装环境: Sql server 2017 + Windows Server 2012 R2 提供两个网上的下载链接: https ...

  5. jmeter接口测试json详解

    本篇围绕jmeter(压力测试工具),请求json与返回json串处理进行解析,初入测试,理解如有不对的地方请大家及时提点~~ 在这里jmeter工具的使用不在做解释 首先说下乱码问题,在这里无脑5步 ...

  6. Android测试monkeyRunner

    monkeyrunner的官方文档: https://developer.android.com/studio/test/monkeyrunner monkeyrunner脚本可以执行截图操作 具体执 ...

  7. Jquery 操作DOM元素

    一.文本输入框: text <input type=”text” value=”99.com” size=12 id=”input1” /> 1.获取文本值: $("#input ...

  8. Java中ClassLoader浅析.

    一.问题 请在Eclipse中新建如下类,并运行它: 1 package java.lang; 2 3 public class Long { 4 public static void main(St ...

  9. [Angular] Using Pipe for function memoization

    Sometimes we might have some expensive function to calcuate state directly from template: <div cl ...

  10. icpc 银川 H. Delivery Route SPFA优化

    Problem Description Pony is the boss of a courier company. The company needs to deliver packages to ...