php file_get_contents與curl性能比較

1.fopen /file_get_contents 每次請求都會從新作DNS查詢,並不對 DNS信息進行緩存。可是CURL會自動對DNS信息進行緩存。對同一域名下的網頁或者圖片的請求只須要一次DNS查詢。這大大減小了DNS查詢的次數。因此CURL的性能比fopen /file_get_contents 好不少。php

2.fopen /file_get_contents 在請求HTTP時,使用的是http_fopen_wrapper,不會keeplive。而curl卻能夠。這樣在屢次請求多個連接時,curl效率會好一些。json

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//這個超時時間不穩定,常常不奏效    )   ));

這時候,看一下服務器的鏈接池,會發現一堆相似的錯誤,讓我頭疼萬分:app

代碼以下 複製代碼
file_get_contents(http://***): failed to open stream…

如今改用了curl庫,寫了一個函數替換: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函數,不但穩定速度快,還能假冒瀏覽器欺騙目標地址哦

再看一個實例

後續貼出了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";

?>

測試訪問

http://www.111cn.net

file_get_contents速度:4.2404510975 seconds curl速度:2.8205530643 seconds

curl比file_get_contents速度快了30%左右,最重要的是服務器負載更低.

總結

file_get_contents處理頻繁小的時候,用它感受挺好的。沒什麼異常。若是你的文件被1k+人處理。那麼你的服務器cpu就等着高升吧。因此建議本身和你們在之後寫php代碼的時候使用curl庫。

相關文章
相關標籤/搜索