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

首頁 > 開發 > PHP > 正文

php獲取網頁請求狀態程序代碼

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

網頁返回狀態代碼很多站長會去查自己網站狀態碼是不是200或錯誤頁面是不是404代碼了,那么我們使用最多的查看方法就是使用站長工具或ff瀏覽器來查,但有很多朋友不知道可以自己寫一個查看狀態代碼的功能.

方法一,使用 fsockopen,嚴重鄙視curl_getinfo,代碼如下:

  1. function get_http_code($url="localhost"$port=80, $fsock_timeout=10){ 
  2.     set_time_limit(0); 
  3.     ignore_user_abort(true); 
  4.  
  5.     // 記錄開始時間 
  6.     list($usec$sec) = explode(" ", microtime(true)); 
  7.     $timer['start'] = (float)$usec + (float)$sec
  8.  
  9.     // 校驗URL 
  10.     if(!preg_match("/^https?:\/\//i"$url)){ 
  11.         $url = "http://".$url
  12.     } 
  13.     // 支持HTTPS 
  14.     if(preg_match("/^https:\/\//i"$url)){ 
  15.         $port = 443; 
  16.     } 
  17.  
  18.     // 解析URL 
  19.     $urlinfo = parse_url($url); 
  20.     if(emptyempty($urlinfo['path'])){ 
  21.         $urlinfo['path'] = '/'
  22.     } 
  23.     $host = $urlinfo['host']; 
  24.     $uri = $urlinfo['path'] . (emptyempty($urlinfo['query'])?'':$urlinfo['query']); 
  25.  
  26.     // 通過fsock打開連接 
  27.     if(!$fp = fsockopen($host$port$errno$error$fsock_timeout)){ 
  28.         list($usec$sec) = explode(" ", microtime(true)); 
  29.         $timer['end'] = (float)$usec + (float)$sec
  30.         $usetime = (float)$timer['end'] - (float)$timer['start']; 
  31.  
  32.         return array('code'=>-1, 'usetime'=>$usetime); 
  33.     } 
  34.  
  35.     // 提交請求 
  36.     $status = socket_get_status($fp); 
  37.     $out = "GET {$uri} HTTP/1.1\r\n"
  38.     $out .= "Host: {$host}\r\n"
  39.     $out .= "Connection: Close\r\n\r\n"
  40.     $write = fwrite($fp$out); 
  41.     if(!$write){ 
  42.         list($usec$sec) = explode(" ", microtime(true)); 
  43.         $timer['end'] = (float)$usec + (float)$sec
  44.         $usetime = (float)$timer['end'] - (float)$timer['start']; 
  45.  
  46.         return array('code'=>-2, 'usetime'=>$usetime); 
  47.     } 
  48.  
  49.     $ret = fgets($fp, 1024); 
  50.     preg_match("/http\/\d\.\d\s(\d+)/i"$ret$m); 
  51.     $code = $m[1]; 
  52.     fclose($fp); 
  53.  
  54.     list($usec$sec) = explode(" ", microtime(true)); 
  55.     $timer['end'] = (float)$usec + (float)$sec
  56.     $usetime = (float)$timer['end'] - (float)$timer['start']; 
  57.  
  58.     return array('code'=>$code'usetime'=>$usetime); 

file_get_contents 是 fsockopen 功能的簡單打包,效率稍低些,但是抓取成功率很高,所以在 snoopy 出問題的時候我一般那他來。5.0.0 添加了對 context 的支持,有了context,他也可以發送 header 信息,自定義用戶 agent, referer, cookies 都不在話下。5.1.0 添加了 offset 和 maxlen 參數,可以只讀文件的一部分內容。

方法二,使用snoopy.class.php

Snoopy是一個php類,用來模擬瀏覽器的功能,可以獲取網頁內容,發送表單,代碼如下:

  1. $ch = curl_init(); 
  2. curl_setopt($ch, CURLOPT_URL, 'http://www.spiegel.de/'); 
  3. curl_setopt($ch, CURLOPT_RANGE, '0-500'); 
  4. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  6. $result = curl_exec($ch); 
  7. curl_close($ch); 
  8. echo $result
  9. /** 
  10. *But as noted before if the server doesn't honor this header but sends the whole file curl will download all of it. E.g. http://www.111cn.net ignores the header. But you can (in addition) set a write function callback and abort the request when more data is received, e.g. 
  11. * php 5.3+ only 
  12. * use function writefn($ch, $chunk) { ... } for earlier versions 
  13. */ 
  14. $writefn = function($ch$chunk) { 
  15.   static $data=''
  16.   static $limit = 500; // 500 bytes, it's only a test 
  17.   $len = strlen($data) + strlen($chunk); 
  18.   if ($len >= $limit ) { 
  19.     $data .= substr($chunk, 0, $limit-strlen($data)); 
  20.     echo strlen($data) , ' '$data
  21.     return -1; 
  22.   } 
  23.   $data .= $chunk
  24.   return strlen($chunk); 
  25. }; 
  26. $ch = curl_init(); 
  27. curl_setopt($ch, CURLOPT_URL, 'http://www.111cn.net/'); 
  28. curl_setopt($ch, CURLOPT_RANGE, '0-500'); 
  29. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
  30. curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn); 
  31. $result = curl_exec($ch); 
  32. curl_close($ch); 

一些常見的狀態碼為:

200 - 服務器成功返回網頁

404 - 請求的網頁不存在

503 - 服務器超時

301 - 頁面重定向

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 左云县| 池州市| 孟州市| 班戈县| 广灵县| 鹿邑县| 阳西县| 潼关县| 日土县| 唐海县| 四子王旗| 津市市| 龙州县| 宁远县| 昌邑市| 达拉特旗| 龙胜| 武冈市| 黄骅市| 嘉鱼县| 海淀区| 湛江市| 贡觉县| 定安县| 佛山市| 瑞丽市| 惠安县| 阳江市| 德州市| 云霄县| 太和县| 招远市| 南靖县| 林周县| 大石桥市| 米泉市| 繁昌县| 太白县| 文昌市| 东源县| 绍兴县|