【问题标题】:How to get server response time using PHP如何使用 PHP 获取服务器响应时间
【发布时间】:2016-03-07 16:44:27
【问题描述】:

我需要一个 php 脚本来检查来自另一台服务器(如 http://www.example.com)的正常 HTTP 响应,一个状态脚本来查看其他服务器是否正常运行。

有人可以帮帮我吗?

【问题讨论】:

  • 发出请求时启动计数器。当你得到响应时停止它。

标签: php server


【解决方案1】:

如果你已经有 url,你可以将它们传递给这个函数,你会得到响应时间:

<?php
// check responsetime for a webbserver
function pingDomain($domain){
    $starttime = microtime(true);
    // supress error messages with @
    $file      = @fsockopen($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file){
        $status = -1;  // Site is down
    }
    else{
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
?>

http://tech.fireflake.com/2008/09/17/using-php-to-check-response-time-of-http-server/

【讨论】:

    【解决方案2】:

    您可以在 php 中简单地借助 cURL。您可以发送请求并查看请求的确切时间。

    <?php
      if(!isset($_GET['url']))
      die("enter url");
      $ch = curl_init($_GET['url']); //get url http://www.xxxx.com/cru.php?url=http://www.example.com
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      if(curl_exec($ch))
      {
      $info = curl_getinfo($ch);
      echo 'Took ' . $info['total_time'] . ' seconds to transfer a request to ' . $info['url'];
      }
    
      curl_close($ch);
    ?>
    

    【讨论】:

    • 非常感谢南都。
    猜你喜欢
    • 2018-09-26
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多