你不能用PHP這樣做。使用time()
或microtime()
您只能獲得一個或多個命令完成的時間。
您需要一個工具,您可以訪問網絡層數據。 cURL可以爲你做到這一點,但你必須enable php curl,如果它尚未完成。
PHP可以比較結果並處理它。
<?php
// Create a cURL handle
$ch = curl_init('http://www.example.com/');
// Execute
curl_exec($ch);
// Check if any error occurred
if (!curl_errno($ch)) {
$info = curl_getinfo($ch);
echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
}
// Close handle
curl_close($ch);
你有$info
一堆信息像
- 「文件時間」
- 「TOTAL_TIME」
- 「namelookup_time」
- 「CONNECT_TIME」
- 「pretransfer_time 「
- 」starttransfer_time「
- 」redirect_time「
完整的列表可以發現here
」等待「 時間應該是starttransfer_time
- pretransfer_time
, 所以你的情況,你需要:
$wait = $info['starttransfer_time'] - $info['pretransfer_time'];
你可能想看看[microtime()](http://php.net/manual/en/function.microtime.php)而不是'時間()'。 – CD001
[如何使用PHP獲取服務器響應時間]可能的副本(https://stackoverflow.com/questions/34059737/how-to-get-server-response-time-using-php) –
如果您想要細粒度像第一個字節時間這樣的統計信息,您需要使用像[Curl](http://php.net/manual/en/book.curl.php)這樣的低級庫。 'curl_get_info'函數將會有你所需要的。 – iainn