【发布时间】:2016-03-07 16:44:27
【问题描述】:
我需要一个 php 脚本来检查来自另一台服务器(如 http://www.example.com)的正常 HTTP 响应,一个状态脚本来查看其他服务器是否正常运行。
有人可以帮帮我吗?
【问题讨论】:
-
发出请求时启动计数器。当你得到响应时停止它。
我需要一个 php 脚本来检查来自另一台服务器(如 http://www.example.com)的正常 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;
}
?>
http://tech.fireflake.com/2008/09/17/using-php-to-check-response-time-of-http-server/
【讨论】:
您可以在 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);
?>
【讨论】: