首頁  >  文章  >  後端開發  >  PHP fopen/file_get_contents與curl效能比較

PHP fopen/file_get_contents與curl效能比較

藏色散人
藏色散人轉載
2019-07-10 14:16:203833瀏覽

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==&#39;1&#39;){ 
        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==&#39;1&#39;){ 
        return false; 
    } 
    $city = $ipinfo->data->region.$ipinfo->data->city; 
    return $city; 
} 
// for file_get_contents 
$startTime=explode(&#39; &#39;,microtime()); 
$startTime=$startTime[0] + $startTime[1]; 
for($i=1;$i<=10;$i++) 
{ 
   echo getCity("121.207.247.202")."</br>"; 
} 
$endTime = explode(&#39; &#39;,microtime()); 
$endTime = $endTime[0] + $endTime[1]; 
$totalTime = $endTime - $startTime; 
echo &#39;file_get_contents:&#39;.number_format($totalTime, 10, &#39;.&#39;, "")." seconds</br>"; 
//for curl 
$startTime2=explode(&#39; &#39;,microtime()); 
$startTime2=$startTime2[0] + $startTime2[1]; 
for($i=1;$i<=10;$i++) 
{ 
   echo getCityCurl(&#39;121.207.247.202&#39;)."</br>"; 
} 
$endTime2 = explode(&#39; &#39;,microtime()); 
$endTime2=$endTime2[0] + $endTime2[1]; 
$totalTime2 = $endTime2 - $startTime2; 
echo "curl:".number_format($totalTime2, 10, &#39;.&#39;, "")." seconds"; 
?>

file_get_contents 速度:4.2404510975 seconds

curl 速度:2.8205530643 seconds

curl 速度:2.8205530643 seconds

curl 速度

1_content#get8%的是伺服器負載更低.

總結

###file_get_contents 處理頻繁小的時候,用它感覺挺好的。沒什麼異常。如果你的文件被 1k 人處理。那麼你的伺服器 cpu 就等著高升吧。所以建議自己和大家在以後寫 php 程式碼的時候使用 curl 函式庫。 ###

以上是PHP fopen/file_get_contents與curl效能比較的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除