国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php file_get_contents與curl性能比較

2024-05-04 21:48:25
字體:
來源:轉載
供稿:網友

在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函數,但是總是會遇到獲取失敗的問題,盡管按照手冊中的例子設置了超時,可多數時候不會奏效,代碼如下:

  1. $config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”, 
  2.    ’timeout’ => 5//這個超時時間不穩定,經常不奏效 
  3.    ) 
  4.   )); 

這時候,看一下服務器的連接池,會發現一堆類似的錯誤,讓我頭疼萬分,代碼如下:

file_get_contents(http://Vevb.com):failed to open stream… 

現在改用了curl庫,寫了一個函數替換,代碼如下:

  1. function curl_file_get_contents($durl){ 
  2.   $ch = curl_init(); 
  3.   curl_setopt($ch, CURLOPT_URL, $durl); 
  4.   curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
  5.   curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); 
  6.   curl_setopt($ch, CURLOPT_REFERER,_REFERER_); 
  7.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  8.   $r = curl_exec($ch); 
  9.   curl_close($ch); 
  10.    return $r

如此,除了真正的網絡問題外,沒再出現任何問題,這是別人做過的關于curl和file_get_contents的測試,file_get_contents抓取google.com需用秒數.

  1. 2.31319094 
  2. 2.30374217 
  3. 2.21512604 
  4. 3.30553889 
  5. 2.30124092 
  6.  
  7. curl使用的時間: 
  8.  
  9. 0.68719101 
  10. 0.64675593 
  11. 0.64326 
  12. 0.81983113 
  13. 0.63956594 

差距很大?呵呵,從我使用的經驗來說,這兩個工具不只是速度有差異,穩定性也相差很大,建議對網絡數據抓取穩定性要求比較高的朋友使用上面的 curl_file_get_contents函數,不但穩定速度快,還能假冒瀏覽器欺騙目標地址.

再看一個實例

后續貼出了curl和file_get_contents的對比結果,這邊除了curl與file_get_contents的性能對比,還包含了他們的性能對比,curl與file_get_contents性能對比PHP源代碼如下:

  1. <?php  
  2. /**  
  3.  
  4. * 通過淘寶IP接口獲取IP地理位置  
  5.  
  6. * @param string $ip  
  7.  
  8. * @return: string  
  9.  
  10. **/ 
  11. function getCityCurl($ip)  
  12. {  
  13.     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;  
  14.     $ch = curl_init();  
  15.     $timeout = 5;  
  16.     curl_setopt ($ch, CURLOPT_URL, $url);  
  17.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  18.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  19.     $file_contents = curl_exec($ch);  
  20.     curl_close($ch);  
  21.    
  22.     $ipinfo=json_decode($file_contents);  
  23.     if($ipinfo->code=='1'){  
  24.         return false;  
  25.     }  
  26.     $city = $ipinfo->data->region.$ipinfo->data->city;  
  27.     return $city;  
  28. }  
  29.    
  30. function getCity($ip)  
  31. {  
  32.     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;  
  33.     $ipinfo=json_decode(file_get_contents($url));  
  34.     if($ipinfo->code=='1'){  
  35.         return false;  
  36.     }  
  37.     $city = $ipinfo->data->region.$ipinfo->data->city;  
  38.     return $city;  
  39. }  
  40.    
  41. // for file_get_contents  
  42. $startTime=explode(' ',microtime());  
  43. $startTime=$startTime[0] + $startTime[1];  
  44. for($i=1;$i<=10;$i++)  
  45. //開源代碼Vevb.com 
  46.    echo getCity("121.207.247.202")."</br>";  
  47. }  
  48. $endTime = explode(' ',microtime());  
  49. $endTime = $endTime[0] + $endTime[1];  
  50. $totalTime = $endTime - $startTime;  
  51. echo 'file_get_contents:'.number_format($totalTime, 10, '.'"")." seconds</br>";  
  52.    
  53. //for curl  
  54. $startTime2=explode(' ',microtime());  
  55. $startTime2=$startTime2[0] + $startTime2[1];  
  56. for($i=1;$i<=10;$i++)  
  57. {  
  58.    echo getCityCurl('121.207.247.202')."</br>";  
  59. }  
  60. $endTime2 = explode(' ',microtime());  
  61. $endTime2=$endTime2[0] + $endTime2[1];  
  62. $totalTime2 = $endTime2 - $startTime2;  
  63. echo "curl:".number_format($totalTime2, 10, '.'"")." seconds";  
  64. ?> 

測試訪問:http://m.survivalescaperooms.com

file_get_contents速度:4.2404510975 seconds

curl速度:2.8205530643 seconds

curl比file_get_contents速度快了30%左右,最重要的是服務器負載更低.

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上蔡县| 武乡县| 台湾省| 温州市| 商城县| 庆城县| 兴文县| 隆尧县| 湟源县| 苍南县| 黄石市| 荣昌县| 抚松县| 高唐县| 西畴县| 大兴区| 平湖市| 太仓市| 淮安市| 卓资县| 孙吴县| 灵石县| 阿克陶县| 涿州市| 固安县| 嘉义市| 诸城市| 聊城市| 比如县| 葫芦岛市| 济宁市| 永康市| 裕民县| 四平市| 伊春市| 金门县| 辽中县| 太仓市| 罗平县| 韶山市| 东乡县|