手机浏览 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条记录相关文章

JS模拟FLASH效果 (浏览: 24381, 评论: 0)
病了,暂不更新 (浏览: 19372, 评论: 3)
创建Yii扩展 (浏览: 19370, 评论: 0)
参加lamp聚会有感 (浏览: 19133, 评论: 1)
转+收藏:LINUX书架 (浏览: 18527, 评论: 0)
基本实现 phprpc_client For SAE (浏览: 18519, 评论: 0)

2条记录访客评论

刷pv没有意义吧

Post by fj on 2011, July 16, 3:10 PM 引用此文发表评论 #1

这个。。。还不完整吗?
1、PHP代码已经有了
2、防止访问的配置也告诉你了啦。
3、cron,这个在yaml文件本身就有,只是要注意一下。yaml是以空格的缩进来处理的。

Post by gouki on 2010, June 26, 3:52 PM 引用此文发表评论 #2

有空写一个完整的教程吧

Post by 花虫虫 on 2010, June 26, 3:28 PM 引用此文发表评论 #3


发表评论

评论内容 (必填):