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机能比较的细致内容,更多请关注ki4网别的相干文章!