php中file_get_contents與curl性能比較分析
本文實(shí)例講述了php中file_get_contents與curl性能比較分析。分享給大家供大家參考。具體如下:
在php中如果不仔細(xì)的去分析性能會(huì)發(fā)現(xiàn)file_get_contents與curl兩個(gè)同很多共同點(diǎn)的,他們都可以采集文件打開(kāi)文件,但是如果仔細(xì)一對(duì)比會(huì)發(fā)現(xiàn)很多不同點(diǎn),下面我們一起來(lái)看看file_get_contents與curl區(qū)別。
PHP中fopen,file_get_contents,curl函數(shù)的區(qū)別:
1.fopen /file_get_contents 每次請(qǐng)求都會(huì)重新做DNS查詢(xún),并不對(duì) DNS信息進(jìn)行緩存。但是CURL會(huì)自動(dòng)對(duì)DNS信息進(jìn)行緩存。對(duì)同一域名下的網(wǎng)頁(yè)或者圖片的請(qǐng)求只需要一次DNS查詢(xún)。這大大減少了DNS查詢(xún)的次數(shù)。所以CURL的性能比f(wàn)open /file_get_contents 好很多。
2.fopen /file_get_contents 在請(qǐng)求HTTP時(shí),使用的是http_fopen_wrapper,不會(huì)keeplive。而curl卻可以。這樣在多次請(qǐng)求多個(gè)鏈接時(shí),curl效率會(huì)好一些。
3.fopen / file_get_contents 函數(shù)會(huì)受到php.ini文件中allow_url_open選項(xiàng)配置的影響。如果該配置關(guān)閉了,則該函數(shù)也就失效了。而curl不受該配置的影響。
4.curl 可以模擬多種請(qǐng)求,例如:POST數(shù)據(jù),表單提交等,用戶(hù)可以按照自己的需求來(lái)定制請(qǐng)求。而fopen / file_get_contents只能使用get方式獲取數(shù)據(jù)。
file_get_contents 獲取遠(yuǎn)程文件時(shí)會(huì)把結(jié)果都存在一個(gè)字符串中 fiels函數(shù)則會(huì)儲(chǔ)存成數(shù)組形式
因此,我還是比較傾向于使用curl來(lái)訪(fǎng)問(wèn)遠(yuǎn)程url。Php有curl模塊擴(kuò)展,功能很是強(qiáng)大。
說(shuō)了半天大家可能說(shuō)性能怎么沒(méi)對(duì)比呢,那我們就來(lái)看看
最近需要獲取別人網(wǎng)站上的音樂(lè)數(shù)據(jù)。用了file_get_contents函數(shù),但是總是會(huì)遇到獲取失敗的問(wèn)題,盡管按照手冊(cè)中的 例子設(shè)置了超時(shí),可多數(shù)時(shí)候不會(huì)奏效:
'timeout' => 5//這個(gè)超時(shí)時(shí)間不穩(wěn)定,經(jīng)常不奏效
)
));
這時(shí)候,看一下服務(wù)器的連接池,會(huì)發(fā)現(xiàn)一堆類(lèi)似的錯(cuò)誤,讓我頭疼萬(wàn)分:
file_get_contents(http://***): failed to open stream…
現(xiàn)在改用了curl庫(kù),寫(xiě)了一個(gè)函數(shù)替換:
$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;
}
如此,除了真正的網(wǎng)絡(luò)問(wèn)題外,沒(méi)再出現(xiàn)任何問(wèn)題。
這是別人做過(guò)的關(guān)于curl和file_get_contents的測(cè)試:
file_get_contents抓取google.com需用秒數(shù):
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
curl使用的時(shí)間:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
差距很大?呵呵,從我使用的經(jīng)驗(yàn)來(lái)說(shuō),這兩個(gè)工具不只是速度有差異,穩(wěn)定性也相差很大。
建議對(duì)網(wǎng)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標(biāo)地址哦
再看一個(gè)實(shí)例
后續(xù)貼出了curl和file_get_contents的對(duì)比結(jié)果,這邊除了curl與file_get_contents的性能對(duì)比,還包含了他們的性能對(duì)比,講之前看下如下的結(jié)果圖:
curl與file_get_contents性能對(duì)比PHP源代碼如下:
/**
* 通過(guò)淘寶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";
?>
測(cè)試訪(fǎng)問(wèn)
http://chabaoo.cn
file_get_contents速度:4.2404510975 seconds
curl速度:2.8205530643 seconds
curl比f(wàn)ile_get_contents速度快了30%左右,最重要的是服務(wù)器負(fù)載更低.
總結(jié)
file_get_contents處理頻繁小的時(shí)候,用它感覺(jué)挺好的。沒(méi)什么異常。如果你的文件被1k+人處理。那么你的服務(wù)器cpu就等著高升吧。所以建議自己和大家在以后寫(xiě)php代碼的時(shí)候使用curl庫(kù)。
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php數(shù)組索引的Key加引號(hào)和不加引號(hào)的區(qū)別
這篇文章主要介紹了php數(shù)組索引的Key加引號(hào)和不加引號(hào)的區(qū)別,加引號(hào)和不加引號(hào)是有嚴(yán)重的區(qū)別的,需要的朋友可以參考下2014-08-08php循環(huán)輸出數(shù)據(jù)庫(kù)內(nèi)容的代碼
今天書(shū)寫(xiě)php的循環(huán)輸出內(nèi)容,總發(fā)現(xiàn)第一篇不能現(xiàn)實(shí),原來(lái)是用php do while語(yǔ)句,后來(lái)改成while所以出現(xiàn)這個(gè)問(wèn)題,都怪學(xué)藝不精啊,特整理下2008-05-05PHP中如何調(diào)用webservice的實(shí)例參考
本篇文章介紹了,PHP中如何調(diào)用webservice的實(shí)例參考。需要的朋友參考下2013-04-04PHP數(shù)據(jù)庫(kù)編程之MySQL優(yōu)化策略概述
這篇文章主要介紹了PHP數(shù)據(jù)庫(kù)編程之MySQL優(yōu)化策略,簡(jiǎn)單講述了mysql優(yōu)化的簡(jiǎn)單技巧以及索引優(yōu)化、查詢(xún)優(yōu)化、存儲(chǔ)優(yōu)化等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08php顏色轉(zhuǎn)換函數(shù)hex-rgb(將十六進(jìn)制格式轉(zhuǎn)成十進(jìn)制格式)
將十六進(jìn)制格式轉(zhuǎn)成十進(jìn)制格式的函數(shù)代碼,也就是hex-rgb顏色轉(zhuǎn)換需要的2013-09-09