How to get server response time using PHP

牧云@^-^@ 提交于 2019-12-13 16:06:30

问题


I needed a script in php that checked for a normal HTTP response from another server like http://www.example.com, a status script to see if the other server(s) where behaving as they should.

Anybody can help me?


回答1:


You can do simply with help of cURL in php. You can send request and view exact time of request.

<?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);
?>



回答2:


If you already have urls you can pass them to this function and you will get the response time:

<?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/



来源:https://stackoverflow.com/questions/34059737/how-to-get-server-response-time-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!