溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP中fopen,file_get_contents,curl函數(shù)有什么區(qū)別

發(fā)布時間:2020-11-12 14:16:02 來源:億速云 閱讀:180 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關PHP中fopen,file_get_contents,curl函數(shù)有什么區(qū)別的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

PHP中fopen,file_get_contents,curl 函數(shù)的區(qū)別:

1.fopen/file_get_contents 每次請求都會重新做 DNS 查詢,并不對 DNS 信息進行緩存。

但是 CURL 會自動對 DNS 信息進行緩存。對同一域名下的網(wǎng)頁或者圖片的請求只需要一次 DNS 查詢。這大大減少了 DNS 查詢的次數(shù)。所以 CURL 的性能比 fopen /file_get_contents 好很多。

2.fopen/file_get_contents 在請求 HTTP 時,使用的是 http_fopen_wrapper,不會 keeplive。

而 curl 卻可以。這樣在多次請求多個鏈接時,curl 效率會好一些。

3.fopen/file_get_contents 函數(shù)會受到 php.ini 文件中 allow_url_open 選項配置的影響。

如果該配置關閉了,則該函數(shù)也就失效了。而 curl 不受該配置的影響。

4.curl 可以模擬多種請求,例如:POST 數(shù)據(jù),表單提交等,用戶可以按照自己的需求來定制請求。

而 fopen /file_get_contents 只能使用 get 方式獲取數(shù)據(jù)。

file_get_contents 獲取遠程文件時會把結果都存在一個字符串中 fiels 函數(shù)則會儲存成數(shù)組形式

因此,我還是比較傾向于使用 curl 來訪問遠程 url。Php 有 curl 模塊擴展,功能很是強大。

說了半天大家可能說性能怎么沒對比呢,那我們就來看看

#最近需要獲取別人網(wǎng)站上的音樂數(shù)據(jù)。用了file_get_contents函數(shù),但是總是會遇到獲取失敗的問題,盡管按照手冊中的 例子設置了超時,可多數(shù)時候不會奏效:
$config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,
   ’timeout’ => 5//這個超時時間不穩(wěn)定,經(jīng)常不奏效
   )
  ));
#這時候,看一下服務器的連接池,會發(fā)現(xiàn)一堆類似的錯誤,讓我頭疼萬分:
file_get_contents(http://***): failed to open stream…
#現(xiàn)在改用了curl庫,寫了一個函數(shù)替換:
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;
}

如此,除了真正的網(wǎng)絡問題外,沒再出現(xiàn)任何問題。

這是別人做過的關于 curl 和 file_get_contents 的測試:

file_get_contents 抓取 google.com 需用秒數(shù):

2.31319094
2.30374217
2.21512604
3.30553889
2.30124092

curl 使用的時間:

0.68719101
0.64675593
0.64326
0.81983113
0.63956594

差距很大?

呵呵,從我使用的經(jīng)驗來說,這兩個工具不只是速度有差異,穩(wěn)定性也相差很大。

建議對網(wǎng)絡數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents 函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標地址哦

再看一個實例

后續(xù)貼出了 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 庫。

感謝各位的閱讀!關于PHP中fopen,file_get_contents,curl函數(shù)有什么區(qū)別就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI