手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

简单的PHP类,判断远程地址状态

首页 > PHP >

啥也不说了。直接上代码了。主要是用来检查路径文件是否存在:

PHP代码
  1. class Headers   
  2. {  
  3.     static private $headers;  
  4.     static private $getmodes = array('fsock','curl');  
  5.     static private $getmode;  
  6.   
  7.     static private $type;  
  8.   
  9.     public static function init ($type = 'status'){  
  10.         self::$type = strToLower$type );  
  11.     }  
  12.   
  13.     public static function get ( $url ,$mode='')  
  14.     {  
  15.         list( $host$uri , $url ) = self::initURL($url);  
  16.         if($mode && in_array( strToLower$mode ), self::$getmodes )){  
  17.             self::$getmode = $mode;  
  18.         }  
  19.         return self::_get( $host , $uri ,$url);  
  20.     }  
  21.   
  22.     private static function initURL ( $url )  
  23.     {  
  24.         $urls = parse_url$url );  
  25.         return array($urls['host'],$urls['path'],$url);  
  26.     }  
  27.   
  28.     private static function _get ( $host , $uri,$url )  
  29.     {  
  30.         if(self::$getmode == 'curl' && function_exists( 'curl_init' )){  
  31.             return self::_curlopen($host,$uri,$url);  
  32.         }else{  
  33.             return self::_fsockopen($host,$uri);  
  34.         }  
  35.     }  
  36.     private static function _curlopen ($host,$uri,$url){  
  37.         $ch = curl_init();  
  38.         curl_setopt($ch, CURLOPT_URL, $url );  
  39.         curl_setopt($ch, CURLOPT_HEADER, 1);  
  40.         curl_setopt($ch, CURLOPT_HTTPGET, 1);  
  41.         curl_setopt($ch,CURLOPT_NOBODY,1);  
  42.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
  43.         curl_setopt($ch, CURLOPT_TIMEOUT, 60);  
  44.         $result = curl_exec($ch);  
  45. //      echo '<pre>';  
  46. //      print_r( curl_getinfo($ch) );  
  47. //      echo '</pre>';  
  48.         curl_close($ch);  
  49.         if(self::$type == 'status'){  
  50.             return subStr$result, 0 , strPos$result"" ) );  
  51.         }  
  52.         return array_filter(explode""$result ));  
  53.     }  
  54.   
  55.     private static function _fsockopen ($host,$uri)  
  56.     {  
  57.         $headers = array();  
  58.         $header=$errno=$errstr='';  
  59.         $fp = fsockopen ($host, 80, $errno$errstr, 60);  
  60.         if ($fp){  
  61.             fputs ($fp"GET $uri HTTP/1.0");  
  62.             if(self::$type == 'status' ){  
  63.                 $get=fgetss$fp );  
  64.                 return trim( $get );  
  65.             }else{  
  66.                 while (!feof($fp)){  
  67.                     $char = fgetc($fp);  
  68.                     if($char === "\n"){  
  69.                         if (ord($header) === 13){  
  70.                             return $headers;  
  71.                         }else{  
  72.                             array_push($headers, trim($header));   
  73.                         }  
  74.                         $header = '';  
  75.                     }else{  
  76.                         $header = $header.$char;  
  77.                     }  
  78.                 }  
  79.             }  
  80.             fclose ($fp);  
  81.         }  
  82.     }  
  83. }  
  84. $s = microtime(1);  
  85. $get = Headers::get('http://www.neatstudio.com/testtt.php','curl');  
  86. echo '<pre>';  
  87. print_r( $get );  
  88. echo '</pre>';  
  89. echo( sprintf( "%0.6f" , (microtime(1)-$s) ) );  

curl函数中我有一段是注释掉的。如果只是为了取状态,直接运行完后curl_getinfo就OK了。然后取其中的http_code,就可以知道当前的状态。
当然。http_code的具体对应可以参考常用http code

【后记】看到BOPO的回复,才发现。。。郁闷,原来get_headers就可以了。我居然忘了这个函数。傻掉了我。唉。。。。不过这也是从PHP5才开始有的函数。看来我对手册的了解度下降了




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: http, remote, http_code

« 上一篇 | 下一篇 »

只显示10条记录相关文章

简单的添加ssh (浏览: 22859, 评论: 0)
继续COMET之旅 (浏览: 21386, 评论: 0)
常用http code (浏览: 19432, 评论: 1)
Mysql: 'reading initial communication packet' (浏览: 18775, 评论: 0)
HTTP 204和205的应用 (浏览: 18379, 评论: 0)

2条记录访客评论

社大知道!

Post by 爆波团队长 on 2010, June 4, 10:24 PM 引用此文发表评论 #1

更简单的办法 get_headers($url); 然后用正则匹配 200

Post by Bopo on 2010, June 4, 1:35 PM 引用此文发表评论 #2


发表评论

评论内容 (必填):