如何使用PHP获取服务器响应时间

如何使用PHP获取服务器响应时间,php,server,Php,Server,我需要一个php脚本来检查来自另一台服务器的正常HTTP响应,比如一个状态脚本,以查看其他服务器是否正常运行 有人可以帮我吗?如果您已经有URL,您可以将它们传递到此函数,您将获得响应时间: <?php // check responsetime for a webbserver function pingDomain($domain){ $starttime = microtime(true); // supress error messages with @ $

我需要一个php脚本来检查来自另一台服务器的正常HTTP响应,比如一个状态脚本,以查看其他服务器是否正常运行


有人可以帮我吗?

如果您已经有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;
}
?>


您只需借助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);
?>


发出请求时启动计数器。当你得到回应的时候就停下来。非常感谢南都。