PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳
《PHP实例:PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题》要点: PHP学习PHP CURL与file_get_contents函数都可以获取远程服务器上的文件保留到本地,但在性能上面两者完全不在同一个级别,下面我先来介绍PHP CURL或file_get_contents函数应用例子,然后再简单的给各位介绍一下它们的一些小区别吧. 推荐办法 CURL获取 <?php $c = curl_init(); $url = 'www.aspzz.cn'; curl_setopt($c,CURLOPT_URL,$url); curl_setopt($c,CURLOPT_RETURNTRANSFER,1); $data = curl_exec($c); curl_close($c); $pos = strpos($data,'utf-8'); if($pos===false){$data = iconv("gbk","utf-8",$data);} preg_match("/<title>(.*)<\/title>/i",$data,$title); echo $title[1]; ?> 使用file_get_contents <?php $content=file_get_contents("/"); $pos = strpos($content,'utf-8'); if($pos===false){$content = iconv("gbk",$content);} $postb=strpos($content,'<title>')+7; $poste=strpos($content,'</title>'); $length=$poste-$postb; echo substr($content,$postb,$length); ?> 看看file_get_contents性能 1)fopen/file_get_contents 每次哀求远程URL中的数据都会重新做DNS查询,并不对DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的哀求只需要一次DNS 查询.这大大减少了DNS查询的次数.所以CURL的性能比fopen/file_get_contents 好很多. curl与file_get_contents性能对比PHP源代码如下: 1829.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,$url); curl_setopt ($ch,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,"")." seconds"; ?> 测试拜访 file_get_contents速度:4.2404510975 seconds ps:php函数file_get_contents与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_get_contents($url) { $ch = curl_init(); curl_setopt($ch,$url); //设置拜访的url地址 //curl_setopt($ch,CURLOPT_HEADER,1); //是否显示头部信息 curl_setopt($ch,CURLOPT_TIMEOUT,5); //设置超时 curl_setopt($ch,CURLOPT_USERAGENT,_USERAGENT_); //用户拜访代理 User-Agent curl_setopt($ch,CURLOPT_REFERER,_REFERER_); //设置 referer curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟踪301 curl_setopt($ch,1); //返回结果 $r = curl_exec($ch); curl_close($ch); return $r; } 如此,除了真正的网络问题外,没再出现任何问题. 这是别人做过的关于curl和file_get_contents的测试: file_get_contents抓取google.com需用秒数: 2.31319094?? curl使用的时间: 0.68719101?? 差距很大吧?呵呵,从我使用的经验来说,这两个工具不只是速度有差异,稳定性也相差很大.建议对网络数据抓取稳定性要求比拟高的朋友使用上面的 curl_file_get_contents函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦! 欢迎参与《PHP实例:PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |