php的file-get-contents與curl性能分析
主要介紹了php中file_get_contents與curl性能比較,以實例形式詳細分析了file_get_contents與curl的區別以及運行效率的對比,需要的朋友可以參考下.就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆畢業生考試網!
本文實例講述了php中file_get_contents與curl性能比較分析。分享給大家供大家參考。具體如下:
在php中如果不仔細的去分析性能會發現file_get_contents與curl兩個同很多共同點的,他們都可以採集文件打開文件,但是如果仔細一對比會發現很多不同點,下面我們一起來看看file_get_contents與curl區別。
PHP中fopen,file_get_contents,curl函數的區別:
n /file_get_contents 每次請求都會重新做DNS查詢,並不對 DNS信息進行緩存。但是CURL會自動對DNS信息進行緩存。對同一域名下的網頁或者圖片的請求只需要一次DNS查詢。這大大減少了DNS查詢的次數。所以CURL的性能比fopen /file_get_contents 好很多。
n /file_get_contents 在請求HTTP時,使用的是http_fopen_wrapper,不會keeplive。而curl卻可以。這樣在多次請求多個鏈接時,curl效率會好一些。
n / file_get_contents 函數會受到文件中allow_url_open選項配置的影響。如果該配置關閉了,則該函數也就失效了。而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抓取需用秒數:
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="$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="$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("")."</br>";
}
$endTime = explode(' ',microtime());
$endTime = $endTime[0] + $endTime[1];
$totalTime = $endTime - $startTime;
echo 'file_get_contents:'er_format($totalTime, 10, '.', "")." seconds</br>";
//for curl
$startTime2=explode(' ',microtime());
$startTime2=$startTime2[0] + $startTime2[1];
for($i=1;$i<=10;$i++)
{
echo getCityCurl('')."</br>";
}
$endTime2 = explode(' ',microtime());
$endTime2=$endTime2[0] + $endTime2[1];
$totalTime2 = $endTime2 - $startTime2;
echo "curl:"er_format($totalTime2, 10, '.', "")." seconds";
?>
相關文章
-
php中file-get-contents與curl性能分析
總結是事後對某一階段的學習、工作或其完成情況加以回顧和分析的一種書面材料,它可以幫助我們有尋找學習和工作中的規律,我想我們需要寫一份總結了吧。但是總結有什麼要求呢?以下是小編爲大家收集的教師國小實習總結,歡迎 -
php中file-get-contents與curl性能比較分析
在php中如果不仔細的去分析性能會發現file_get_contents與curl兩個同很多共同點的,他們都可以採集文件打開文件,但是如果仔細一對比會發現很多不同點,以下是小編爲大家搜索整理的php中file_get_contents與curl性能比較分 -
php中file-get-contents與curl性能比較分析的內容
PHP中fopen,file_get_contents,curl函數的區別:n /file_get_contents 每次請求都會重新做DNS查詢,並不對 DNS信息進行緩存。但是CURL會自動對DNS信息進行緩存。對同一域名下的網頁或者圖片的`請求只需要一次DNS查詢。這 -
php中file-get-contents與curl性能比較
服裝網絡營銷工作計劃引言在我國服裝營銷這一領域的理論研究與應用剛剛起步,本論文的目的是從宏觀的角度討論整個服裝業目前的發展,再推及到杭州魅力女裝網絡營銷活動方案,挖掘在服裝行業各個層次的內在關需求和潛在市場 -
The Best Thing That Could Ever Happen少兒雙語閱讀
Unit 3 The Best Thing That Could Ever Happen單元 3 最棒的事Three old men were sitting around and talking.三個老頭坐在一起聊天。The 80-year-old remarked, "The best thing that could happen to me would j -
The Unforgettable School Trip英語作文
the unforgettable school triplast tuesday, i went to chimelong paradise with my classmates. i played many different and exciting games. we arrived there at about 9 o’clock. there were 3 people in my -
淺析javascript中function 的length屬性
[1,2,3]th 可以得到 3, "123"th 也可以得到 3,這個略懂js的都知道。但是 th,th,""th,th 會得到什麼呢?分別得到 1,2,0,1,這些數字代表什麼呢?這個是羣裏很多新人朋友一直問的一個問題,其實函數的 length 得到的是形參個數。我們來 -
Ought Parents to Give Children Pocket英語作文
We live in a commercial society. Everything that we use is merchandise. In the commercial world the media is money. Therefore, money is very important to us. Eaing money and spending money are big pro -
The place of Science and Technology in Modern Life英語作文
human life can not continue without science and technology. for many years, human society has developed with the advance of science and technology while the development of science and technology has i -
高中英語作文:My approach to difficulties in learning
某校英文報社開設了一個專欄:experience . 本期話題是如何解決學習中遇到的`困難。請你以“my approach to difficulties in leaing” 爲題,用英語寫一篇短文,談談自己的一些做法。 範文: My approach to difficulties