实现Ping功能的PHP类 - HTTP Ping Class

fmms 8年前
     这是一个简单的PHP脚本,可用于Ping网站是否可用。    <pre class="brush:php; toolbar: true; auto-links: false;"><?php  /*************************************************************************  * http_ping.class.php                                                   *  *************************************************************************  *                                                                       *  * (c) 2008-2011 Wolf Software Limited <support@wolf-software.com>       *  * All Rights Reserved.                                                  *  *                                                                       *  * This program is free software: you can redistribute it and/or modify  *  * it under the terms of the GNU General Public License as published by  *  * the Free Software Foundation, either version 3 of the License, or     *  * (at your option) any later version.                                   *  *                                                                       *  * This program is distributed in the hope that it will be useful,       *  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *  * GNU General Public License for more details.                          *  *                                                                       *  * You should have received a copy of the GNU General Public License     *  * along with this program.  If not, see <http://www.gnu.org/licenses/>. *  *                                                                       *  *************************************************************************/  class Http_Ping {   private $class_name    = "Http Ping";   private $class_version = "1.1.0";   private $class_author  = "Wolf Software";   private $class_source  = "http://www.wolf-software.com/downloads/php-classes/networking-classes/http-ping-class/";    private $error         = null;    private $error_codes   = Array(                                  Array('code' => 100, 'message' => 'Continue'),                                  Array('code' => 101, 'message' => 'Switching Protocols'),                                  Array('code' => 200, 'message' => 'OK'),                                  Array('code' => 201, 'message' => 'Created'),                                  Array('code' => 202, 'message' => 'Accepted'),                                  Array('code' => 203, 'message' => 'Non-Authoritative Information'),                                  Array('code' => 204, 'message' => 'No Content'),                                  Array('code' => 205, 'message' => 'Reset Content'),                                  Array('code' => 206, 'message' => 'Partial Content'),                                  Array('code' => 300, 'message' => 'Multiple Choices'),                                  Array('code' => 301, 'message' => 'Moved Permanently'),                                  Array('code' => 302, 'message' => 'Found'),                                  Array('code' => 303, 'message' => 'See Other'),                                  Array('code' => 304, 'message' => 'Not Modified'),                                  Array('code' => 305, 'message' => 'Use Proxy'),                                  Array('code' => 306, 'message' => '(Unused)'),                                  Array('code' => 307, 'message' => 'Temporary Redirect'),                                  Array('code' => 400, 'message' => 'Bad Request'),                                  Array('code' => 401, 'message' => 'Unauthorized'),                                  Array('code' => 402, 'message' => 'Payment Required'),                                  Array('code' => 403, 'message' => 'Forbidden'),                                  Array('code' => 404, 'message' => 'Not Found'),                                  Array('code' => 405, 'message' => 'Method Not Allowed'),                                  Array('code' => 406, 'message' => 'Not Acceptable'),                                  Array('code' => 407, 'message' => 'Proxy Authentication Required'),                                  Array('code' => 408, 'message' => 'Request Timeout'),                                  Array('code' => 409, 'message' => 'Conflict'),                                  Array('code' => 410, 'message' => 'Gone'),                                  Array('code' => 411, 'message' => 'Length Required'),                                  Array('code' => 412, 'message' => 'Precondition Failed'),                                  Array('code' => 413, 'message' => 'Request Entity Too Large'),                                  Array('code' => 414, 'message' => 'Request-URI Too Long'),                                  Array('code' => 415, 'message' => 'Unsupported Media Type'),                                  Array('code' => 416, 'message' => 'Requested Range Not Satisfiable'),                                  Array('code' => 417, 'message' => 'Expectation Failed'),                                  Array('code' => 500, 'message' => 'Internal Server Error'),                                  Array('code' => 501, 'message' => 'Not Implemented'),                                  Array('code' => 502, 'message' => 'Bad Gateway'),                                  Array('code' => 503, 'message' => 'Service Unavailable'),                                  Array('code' => 504, 'message' => 'Gateway Timeout'),                                  Array('code' => 505, 'message' => 'HTTP Version Not Supported'),                                 );    public function class_name()     {       return $this->class_name;     }    public function class_version()     {       return $this->class_version;     }    public function class_author()     {       return $this->class_author;     }    public function class_source()     {       return $this->class_source;     }    public function __construct()     {     }    public function ping($url, $port = 80, $ssl = false, $user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)')     {       $this->reset_error();       clearstatcache();        if (substr($url, 0, 4) != "http")         {           if ($ssl)             {               $url = "https://" . $url;             }           else             {               $url = "http://" . $url;             }         }       $ch = curl_init($url);       curl_setopt($ch, CURLOPT_PORT, $port);       curl_setopt($ch, CURLOPT_HEADER, 1);       curl_setopt($ch, CURLOPT_NOBODY, 1);       curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);       curl_setopt($ch, CURLOPT_FAILONERROR, 1);       curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);       curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        $execute = curl_exec($ch);       $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);        if (($http_status >= 200) && ($http_status < 400))         {           $total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);           $ret = $total_time;         }       else         {           if ($http_status == 0)             {               $msg = curl_error($ch);               $this->set_error($msg);               $ret = false;             }           else             {               foreach ($this->error_codes as $error)                 {                   if ($error['code'] == $http_status)                     {                       $this->set_error($error['message']);                       $ret = false;                       break;                     }                 }             }         }       curl_close($ch);       return $ret;     }    private function reset_error()     {       $this->error = null;     }    private function set_error($msg)     {       $this->error = $msg;     }    public function get_error()     {       return $this->error;     } }  ?></pre>使用方法:    <br />    <pre class="brush:php; toolbar: true; auto-links: false;"><?php  require_once 'http_ping.class.php'; $h = new Http_Ping();  echo "Class Name: " . $h->class_name() . "<br>"; echo "Class Version: " . $h->class_version() . "<br>"; echo "Class Author: " . $h->class_author() . "<br>"; echo "Class Source: " . $h->class_source() . "<br><br>";  $hosts = Array(                 Array('name' => 'Wolf Software',       'url' => 'www.wolf-software.com'),                 Array('name' => 'BBC',                 'url' => 'www.bbc.co.uk'),                 Array('name' => 'Google',              'url' => 'www.google.com'),                 Array('name' => 'Intentional Failure', 'url' => 'www.thiswontworkatall.com'),                );  foreach ($hosts as $host)   {     echo "Pinging Host: " . $host['name'] . " = ";     if (($msg = $h->ping($host['url'])) == false)       {         echo $h->get_error() . "<BR>";       }     else       {         echo "$msg<BR>";       }   }  ?></pre>    <br />