file_get_contents与curl的比较(转)
转载;http://blog.sina.com.cn/s/blog_4ce69a220100lm1a.html
以前在读取远程文件的时候,我一直喜欢使用file_get_contents,原因是在PHP的手册里面介绍读取文
件内容的时候,首选此函数。但是现在发现,如果是远程文件,curl才是首选。file_get_contents用来
读取本地文件才是首选。
<wbr></wbr>
首先说性能,下面在一个在国外网站上看到的二者的读取google.com首页的测试:
<wbr></wbr>
Fetching google.com using file_get_contents took(in seconds):
<wbr></wbr>
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
<wbr></wbr>
CURLtook:
<wbr></wbr>
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
<wbr></wbr>
具体还没有测试,不过可以参考一下。
<wbr></wbr>
二是两者除了性能以外,确实还有一些其他的区别,这里顺便说说fopen:
<wbr></wbr>
fopen不会keepalive,
file_get_contents请求http时,使用的是http_fopen_wrapper,也不会keeplive,
<wbr></wbr>
而curl却是可以的。
<wbr></wbr>
这个是读取一般文件的时候并没有什么问题,但是在读取远程问题的时候就会出现问题。如果是要打一个
持续连接,多次请求多个页面。那么file_get_contents和fopen就会出问题。取得的内容也可能会不
对。所以做一些类似采集工作的时候,肯定就有问题了。
<wbr></wbr>
注意到了这个问题,以后如果要给系统做采集功能的时候,就用得着了。
分享到:
相关推荐
本文实例讲述了php中file_get_contents与curl性能比较分析。分享给大家供大家参考。具体如下: 在php中如果不仔细的去分析性能会发现file_get_contents与curl两个同很多共同点的,他们都可以采集文件打开文件,但是...
*/ function curl_get_contents($url,$timeout=1) { $curlHandle = curl_init(); curl_setopt( $curlHandle , CURLOPT_URL, $url ); curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $curl...
下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:< ?php$file_contents = file_get_contents(‘https://www.jb51.net’);echo $file_...
file_get_contents代替使用curl其实不多见了,但有时你碰到服务器不支持curl时我们可以使用file_get_contents代替使用curl,下面看个例子。 当用尽一切办法发现 服务器真的无法使用curl时。或者curl不支持https时。...
如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! 使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置allow_url_fopen = On,allow_url...
下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:< ?php$file_contents = file_get_contents(‘https://www.jb51.net’);echo $file_...
另外,curl的性能比file_get_contents高,所以用curl重写file_get_contents function _file_get_contents($s) { $ret = ; $ch = curl_init($s); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch...
错误: Warning: fopen() [function.fopen]: Unable to find the wrapper “https” – did you forget to enable it ...3.如果服务器你不能修改配置的话,那么就使用curl函数来替代file_get_contents函数,
关于file_get_content 和curl以及fopen 的效率问题,小编比较倾向于使用curl来访问远程url。Php有curl模块扩展,功能很是强大。没事可以研究一下。
今天小编就为大家分享一篇解决PHP curl或file_get_contents下载图片损坏或无法打开的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
$ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //在需要用户检测的网页里需要增加...
抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看...php中curl和file_get_content的一些比较 主要区别: 学习才发现,curl支持很多协议,有FTP, FTPS, HTTP, HTTPS
PHP的file_get_contents获取远程页面内容,如果是gzip编码过的,返回的字符串就是编码后的乱码1、解决方法,找个ungzip的函数来转换下2、给你的url加个前缀,这样调用$content = file_get_contents(“compress....
今天因工作需要,需要用 curl / file_get_contents 获取需要授权(Authorization)的页面内容,解决后写了这篇文章分享给大家。 PHP curl 扩展,能够在服务器端发起POST/GET请求,访问页面,并能获取页面的返回数据。 ...