PHP CURL或file_get_contents擷取網頁標題的代碼及兩者效率的穩定性問題_php執行個體

來源:互聯網
上載者:User

PHP CURL與file_get_contents函數都可以擷取遠程伺服器上的檔案儲存到本地,但在效能上面兩者完全不在同一個層級,下面我先來介紹PHP CURL或file_get_contents函數應用例子,然後再簡單的給各位介紹一下它們的一些小區別吧。

推薦方法 CURL擷取

<?php$c = curl_init();$url = 'www.jb51.net';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("http://www.jb51.net/");$pos = strpos($content,'utf-8');if($pos===false){$content = iconv("gbk","utf-8",$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 好很多。
2)fopen/file_get_contents在請求HTTP時,使用的是http_fopen_wrapper,不會keeplive。而curl卻可以。這樣在多次請求多個連結時,curl效率會好一些。(設定header頭應該可以)
3)fopen/file_get_contents函數會受到php.ini檔案中allow_url_open選項配置的影響。如果該配置關閉了,則該函數也就失效了。而curl不受該配置的影響。
4)curl可以類比多種請求,例如:POST資料,表單提交等,使用者可以按照自己的需求來定製請求。而fopen/file_get_contents只能使用get方式擷取資料。
5)fopen/file_get_contents 不能正確下載二進位檔案
6)fopen/file_get_contents 不能正確處理ssl請求
7)curl 可以利用多線程
8)使用 file_get_contents 的時候如果 網路出現問題, 很容易堆積一些進程在這裡
9)如果是要打一個持續串連,多次請求多個頁面。那麼file_get_contents就會出問題。取得的內容也可能會不對。所以做一些類似採集工作的時候,肯定就有問題了。對做採集抓取的用curl,如果還有同不相信下面我們再做個測試

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, 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%左右,最重要的是伺服器負載更低.

ps:php函數file_get_contents與curl效率及穩定性問題

習慣了使用方便快捷的file_get_contents函數抓取別家網站內容,但是總是會遇到擷取失敗的問題,儘管按照手冊中的例子設定了逾時,可多數時候不好使:

$config['context'] = stream_context_create(array('http' => array('method' => "GET",'timeout' => 5))); 
'timeout' => 5//這個逾時時間不穩定,經常不好使。這時候,看一下伺服器的串連池,會發現一堆類似下面的錯誤,讓你頭疼萬分:

file_get_contents(http://***): failed to open stream… 

不得已,安裝了curl庫,寫了一個函數替換:

function curl_get_contents($url) {  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL, $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, 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函數,不但穩定速度快,還能假冒瀏覽器欺騙目標地址哦!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.