【问题标题】:Get the site status - up or down获取站点状态 - 向上或向下
【发布时间】:2012-04-06 16:55:42
【问题描述】:
<?php
$host = 'http://google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
?>

我正在使用上述程序来获取网站的状态。但我总是收到一条离线消息。 代码有错误吗?

【问题讨论】:

标签: php web


【解决方案1】:

主机名不包含http://,这只是 URI 的方案。

删除它并试试这个:

<?php
$host = 'google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
}
?>

【讨论】:

    【解决方案2】:

    应该是

    if(fsockopen($host, 80, $errno, $errstr, 30)) {
    ...
    

    不是

    if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
    ...
    

    但是 curl 会更好

    【讨论】:

    • 当我尝试上面的代码时,我收到以下错误:警告:fsockopen():无法连接到google.com:80(无法找到套接字传输“http” - 你忘记启用它了吗?当你配置 PHP 时?) /stchecker.php 在第 4 行离线。
    【解决方案3】:

    curl 解决方案怎么样?

    function checkOnline($domain) {
       $curlInit = curl_init($domain);
       curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
       curl_setopt($curlInit,CURLOPT_HEADER,true);
       curl_setopt($curlInit,CURLOPT_NOBODY,true);
       curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
    
       //get answer
       $response = curl_exec($curlInit);
    
       curl_close($curlInit);
       if ($response) return true;
       return false;
    }
    if(checkOnline('http://google.com')) { echo "yes"; }
    

    【讨论】:

      【解决方案4】:

      我知道这是一篇旧帖子,但您也可以解析以下内容的输出:

      $header_check = get_headers("http://www.google.com");
      $response_code = $header_check[0];
      

      这将为您提供完整的 HTTP 状态。

      【讨论】:

        【解决方案5】:

        这可以更快地工作

        <?php
        $domain = '5wic.com';
        if(gethostbyname($domain) != $domain )
        {
        echo "Up";
        }
        else
        {
        echo "Down";
        }
        ?>
        

        【讨论】:

        • 很遗憾,这不是最佳解决方案。当服务器关闭时,由于 DNS 解析,这可能仍表示网站已启动一段时间。我会选择上面@stewe 的解决方案。还有更多含义,请查看 php 网站 php.net/manual/en/function.gethostbyname.php 。谢谢
        【解决方案6】:

        这比 fsock 更快。

        $url = 'https://google.com';
        $url = gethostbyname(str_replace(['https://','http://'],NULL,$url));
        if(filter_var($url,FILTER_VALIDATE_IP)) {
           // online!
        }
        else {
           // offline :(
        }
        

        【讨论】:

          【解决方案7】:

          你可以用这个:

          function is_on($domain) {
            if(gethostbyname($domain) === gethostbyname("testillegaldomain.tld")) {
              return false;
            } else {
              return true;
            }
          }
          

          这会获取一个不存在的域的 IP 地址并将其与您的域的 IP 进行比较;如果相等,则站点关闭,否则打开。

          另外,使用这个函数时不要包含https://前缀!

          【讨论】:

            【解决方案8】:

            我正在为此使用响应状态代码。这将为实时和临时不可用的网站返回 true。

            public function checkLive($url)
            {
                $ch = curl_init();
                curl_setopt ($ch, CURLOPT_URL, $url);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($ch, CURLOPT_TIMEOUT, 15);
                $http_respond = trim(strip_tags(curl_exec($ch)));
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            
                curl_close($ch);
                if (in_array($http_code, ["200", "302"])) {
                    return true;
                } else {
                    return false;
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-09-06
              相关资源
              最近更新 更多