php file_get_contents與curl效能比較
PHP中fopen,file_get_contents,curl函式的區別:
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庫。
本作品採用《CC 協議》,轉載必須註明作者和本文連結