我需要一個php腳本,用於檢查來自另一個服務器(如http://www.example.com)的正常HTTP響應,這是一個狀態腳本,用於查看其他服務器的行爲是否正常。如何使用PHP獲取服務器響應時間
有人可以幫助我嗎?
我需要一個php腳本,用於檢查來自另一個服務器(如http://www.example.com)的正常HTTP響應,這是一個狀態腳本,用於查看其他服務器的行爲是否正常。如何使用PHP獲取服務器響應時間
有人可以幫助我嗎?
您可以在PHP捲曲的幫助下簡單地做。您可以發送請求並查看確切的請求時間。
<?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);
?>
非常感謝Nandu。 – user3305431
如果你已經擁有的網址,你可以將它們傳遞給這個函數,你會得到響應時間:
<?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/
始終爲-1的狀態。 – user3305431
發出請求時啓動計數器。當你得到迴應時停下來。 – Technotronic