欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

原州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

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 選項配置的影響。

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

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

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

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

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

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

#最近需要獲取別人網(wǎng)站上的音樂數(shù)據(jù)。用了file_get_contents函數(shù),但是總是會遇到獲取失敗的問題,盡管按照手冊中的 例子設(shè)置了超時,可多數(shù)時候不會奏效:
$config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,
   ’timeout’ => 5//這個超時時間不穩(wěn)定,經(jīng)常不奏效
   )
  ));
#這時候,看一下服務(wù)器的連接池,會發(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)絡(luò)問題外,沒再出現(xiàn)任何問題。

這是別人做過的關(guā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)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents 函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標(biāo)地址哦

再看一個實例

后續(xù)貼出了 curl 和 file_get_contents 的對比結(jié)果,這邊除了 curl 與 file_get_contents 的性能對比,還包含了他們的性能對比,講之前看下如下的結(jié)果圖:

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% 左右,最重要的是服務(wù)器負(fù)載更低.

總結(jié)

file_get_contents 處理頻繁小的時候,用它感覺挺好的。沒什么異常。如果你的文件被 1k + 人處理。那么你的服務(wù)器 cpu 就等著高升吧。所以建議自己和大家在以后寫 php 代碼的時候使用 curl 庫。

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

分享名稱:PHP中fopen,file_get_contents,curl函數(shù)有什么區(qū)別
本文URL:http://chinadenli.net/article44/gidsee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、自適應(yīng)網(wǎng)站、營銷型網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站企業(yè)建站、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化