<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            php語言

            php中file-get-contents與curl性能比較

            時間:2025-04-22 20:28:18 php語言 我要投稿

            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函數的區別:

              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的性能對比,

            【php中file-get-contents與curl性能比較】相關文章:

            php中file-get-contents與curl性能比較分析05-21

            php中file-get-contents與curl性能分析02-28

            php的file-get-contents與curl性能分析02-18

            PHP中CURL的幾個經典應用08-12

            php中的curl使用入門教程06-20

            PHP前端開發中的性能05-25

            淺談php中curl和fsockopen的應用05-07

            php的curl學習總結07-08

            php中curl模擬post請求小實例06-01

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看