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

首頁 > 語言 > PHP > 正文

PHP CURL或file_get_contents獲取網頁標題的代碼

2024-09-04 11:49:10
字體:
來源:轉載
供稿:網友

PHP CURL與file_get_contents函數都可以獲取遠程服務器上的文件保存到本地,但在性能上面兩者完全不在同一個級別,下面我先來介紹PHP CURL或file_get_contents函數應用例子,然后再簡單的給各位介紹一下它們的一些小區別吧.

推薦方法 CURL獲取,代碼如下:

  1. <?php 
  2. $c = curl_init(); 
  3. $url = 'www.survivalescaperooms.com'
  4. curl_setopt($c, CURLOPT_URL, $url); 
  5. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
  6. $data = curl_exec($c); 
  7. curl_close($c); 
  8. $pos = strpos($data,'utf-8'); 
  9. if($pos===false){$data = iconv("gbk","utf-8",$data);} 
  10. preg_match("/<title>(.*)<\/title>/i",$data$title); 
  11. echo $title[1]; 
  12. ?> 

使用file_get_contents,代碼如下:

  1. <?php 
  2. $content=file_get_contents("http://www.survivalescaperooms.com/"); 
  3. $pos = strpos($content,'utf-8'); 
  4. if($pos===false){$content = iconv("gbk","utf-8",$content);} 
  5. $postb=strpos($content,'<title>')+7; 
  6. $poste=strpos($content,'</title>'); 
  7. $length=$poste-$postb
  8. echo substr($content,$postb,$length); 
  9. ?> 

看看file_get_contents性能

1)fopen/file_get_contents 每次請求遠程URL中的數據都會重新做DNS查詢,并不對DNS信息進行緩存。但是CURL會自動對DNS信息進行緩存。對同一域名下的網頁或者圖片的請求只需要一次DNS 查詢。這大大減少了DNS查詢的次數。所以CURL的性能比fopen/file_get_contents 好很多。

2)fopen/file_get_contents在請求HTTP時,使用的是http_fopen_wrapper,不會keeplive。而curl卻可以。這樣在多次請求多個鏈接時,curl效率會好一些。(設置header頭應該可以)

3)fopen/file_get_contents函數會受到php.ini文件中allow_url_open選項配置的影響。如果該配置關閉了,則該函數也就失效了。而curl不受該配置的影響。

4)curl可以模擬多種請求,例如:POST數據,表單提交等,用戶可以按照自己的需求來定制請求。而fopen/file_get_contents只能使用get方式獲取數據。

5)fopen/file_get_contents 不能正確下載二進制文件

6)fopen/file_get_contents 不能正確處理ssl請求

7)curl 可以利用多線程

8)使用 file_get_contents 的時候如果網絡出現問題,很容易堆積一些進程在這里

9)如果是要打一個持續連接,多次請求多個頁面,那么file_get_contents就會出問題。取得的內容也可能會不對,所以做一些類似采集工作的時候,肯定就有問題了,對做采集抓取的用curl,如果還有同不相信下面我們再做個測試.

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.     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip
  13.     $ch = curl_init(); 
  14.     $timeout = 5; 
  15.     curl_setopt ($ch, CURLOPT_URL, $url); 
  16.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  17.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
  18.     $file_contents = curl_exec($ch); 
  19.     curl_close($ch); 
  20.  
  21.     $ipinfo=json_decode($file_contents); 
  22.     if($ipinfo->code=='1'){ 
  23.         return false; 
  24.     } 
  25.     $city = $ipinfo->data->region.$ipinfo->data->city; 
  26.     return $city
  27.  
  28. function getCity($ip
  29.     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip
  30.     $ipinfo=json_decode(file_get_contents($url)); 
  31.     if($ipinfo->code=='1'){ 
  32.         return false; 
  33.     } 
  34.     $city = $ipinfo->data->region.$ipinfo->data->city; 
  35.     return $city
  36.  
  37. // for file_get_contents 
  38. $startTime=explode(' ',microtime()); 
  39. $startTime=$startTime[0] + $startTime[1]; 
  40. for($i=1;$i<=10;$i++) 
  41.    echo getCity("121.207.247.202")."</br>"
  42. $endTime = explode(' ',microtime()); 
  43. $endTime = $endTime[0] + $endTime[1]; 
  44. $totalTime = $endTime - $startTime
  45. echo 'file_get_contents:'.number_format($totalTime, 10, '.'"")." seconds</br>"
  46.  
  47. //for curl 
  48. $startTime2=explode(' ',microtime()); 
  49. $startTime2=$startTime2[0] + $startTime2[1]; 
  50. for($i=1;$i<=10;$i++) 
  51. //開源軟件:Vevb.com 
  52.    echo getCityCurl('121.207.247.202')."</br>"
  53. $endTime2 = explode(' ',microtime()); 
  54. $endTime2=$endTime2[0] + $endTime2[1]; 
  55. $totalTime2 = $endTime2 - $startTime2
  56. echo "curl:".number_format($totalTime2, 10, '.'"")." seconds"
  57. ?> 

測試訪問:

file_get_contents速度:4.2404510975 seconds

curl速度:2.8205530643 seconds

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泾阳县| 吉木萨尔县| 武城县| 石渠县| 丰顺县| 东山县| 齐河县| 永善县| 章丘市| 九龙坡区| 南京市| 澄迈县| 九寨沟县| 中宁县| 资溪县| 丽水市| 海宁市| 海阳市| 从化市| 定州市| 靖宇县| 景宁| 剑川县| 广德县| 大悟县| 兰西县| 黄浦区| 上虞市| 台中市| 阿荣旗| 陆河县| 呼玛县| 红河县| 阳春市| 金堂县| 柘城县| 色达县| 盐城市| 荣昌县| 阿勒泰市| 四平市|