【问题标题】:Ping site and return result in PHPPing 站点并在 PHP 中返回结果
【发布时间】:2010-11-17 08:47:35
【问题描述】:

我想创建一个小的 IF 过程来检查 Twitter 是否可用(例如,不像现在),并返回 true 或 false。

帮助:)

【问题讨论】:

  • 不能ping,不能发出http请求吗?

标签: php ping


【解决方案1】:

这是一个:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786

另一个:

function ping($host, $port, $timeout) { 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

//Echoing it will display the ping if the host is up, if not it'll say "down".
echo ping("www.google.com", 80, 10);  

【讨论】:

  • 这不是好的返回值。为什么不返回 0/false/null 失败并返回一个表示成功毫秒数的整数?
  • @Philippe Gerber - 因为不是我写的,但这些都是很好的建议。
  • Ping 正在使用 ICMP 协议,没有“端口”之类的东西。您可以 ping 具有 0 个开放 tcp 端口的主机。
  • fsockopen 在未连接互联网时无法在本地主机上运行。它显示此错误警告:fsockopen(): php_network_getaddresses: getaddrinfo failed: No such host is known
  • @karim79 非常感谢。不得不在很短的时间内在项目的生产模式下直接在紧急情况下使用它。照原样使用它。救了我的命。
【解决方案2】:
function urlExists($url=NULL)  
{  
    if($url == NULL) return false;  
    $ch = curl_init($url);  
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $data = curl_exec($ch);  
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    curl_close($ch);  
    if($httpcode>=200 && $httpcode<300){  
        return true;  
    } else {  
        return false;  
    }  
}  

这是从this post 获取的关于如何检查 URL 是否存在的内容。因为 Twitter 在维护时应该提供高于 300 的错误消息,或者是 404,所以这应该可以正常工作。

【讨论】:

【解决方案3】:

ping 几乎适用于所有操作系统。所以你可以进行系统调用并获取结果。

【讨论】:

    【解决方案4】:

    使用shell_exec

    <?php
    $output = shell_exec('ping -c1 google.com');
    echo "<pre>$output</pre>";
    ?>
    

    【讨论】:

    • 你应该在 Linux 上使用 ping -c1 host 或其他东西。普通的ping host 不会返回那里。
    • 更好:if ( 0 != ( $_result = `ping -q -c1 google.com &gt;/dev/null 2&gt;&amp;1 ; echo $?` ) ) { echo 'Fail.'; }
    • 不错,但出于安全原因,许多生产站点总是禁用 PHP shell_exec 功能。
    【解决方案5】:

    另一个选项(如果您需要/想要 ping 而不是发送 HTTP 请求)是 Ping class for PHP。我写它就是为了这个目的,它允许您使用三种受支持的方法之一来 ping 服务器(某些服务器/环境只支持这三种方法中的一种)。

    示例用法:

    require_once('Ping/Ping.php');
    $host = 'www.example.com';
    $ping = new Ping($host);
    $latency = $ping->ping();
    if ($latency) {
      print 'Latency is ' . $latency . ' ms';
    }
    else {
      print 'Host could not be reached.';
    }
    

    【讨论】:

      【解决方案6】:

      使用以下函数,您只是使用socket_create 发送纯ICMP 数据包。我从a user note那里得到了以下代码。注:您必须以 root 身份运行以下命令。

      虽然您不能将其放在标准网页中,但您可以将其作为 cron 作业运行并使用结​​果填充数据库。

      因此,如果您需要监控网站,它最适合。

      function twitterIsUp() {
          return ping('twitter.com');
      }
      
      function ping ($host, $timeout = 1) {
          /* ICMP ping packet with a pre-calculated checksum */
          $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
          $socket = socket_create(AF_INET, SOCK_RAW, 1);
          socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
          socket_connect($socket, $host, null);
      
          $ts = microtime(true);
          socket_send($socket, $package, strLen($package), 0);
          if (socket_read($socket, 255)) {    
              $result = microtime(true) - $ts;
          } else {
              $result = false;
          }
          socket_close($socket);
      
          return $result;
      }
      

      【讨论】:

        【解决方案7】:

        这是我用的php代码,回复一般是这样的:

        2个包发送,2个接收,0%丢包,时间1089ms

        所以我使用了这样的代码:

        $ping_how_many = 2; $ping_result = shell_exec('ping -c '.$ping_how_many.' bing.com'); if( !preg_match('/'.$ping_how_many.' received/',$ping_result) ){ echo '不良 ping 结果'。 PHP_EOL; // 转到下一个1; }

        【讨论】:

        • 这可能会导致ping: icmp open socket: Permission denied。为了解决这个问题,SELinux 必须是 Permissive
        • 对于生产站点在 php 中使用 shell_exec 也不是一个好主意。
        猜你喜欢
        • 2011-12-23
        • 2017-08-23
        • 1970-01-01
        • 2016-04-26
        • 2020-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多