2016-02-07 151 views
1

我可以使用curl獲取URL的HTTP狀態代碼,我可以做類似下面得到URL的響應時間...PHP得到的響應時間和HTTP狀態代碼相同的請求

<?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狀態碼的響應時間。如果這隻可能通過捲曲來實現,那就太棒了。

注意:我不希望/需要URL中的任何其他信息,因爲這會減慢我的過程。

回答

0

請使用get_headers()函數將返回你的狀態代碼,請參閱PHP文檔 - http://php.net/manual/en/function.get-headers.php

<?php 

$url = "http://www.example.com"; 
$header = get_headers($url); 
print_r($header); 
$status_code = $header[0]; 
echo $status_code; 
?> 

Output --> 

Array 
(
    [0] => HTTP/1.0 200 OK 
    [1] => Cache-Control: max-age=604800 
    [2] => Content-Type: text/html 
    [3] => Date: Sun, 07 Feb 2016 13:04:11 GMT 
    [4] => Etag: "359670651+gzip+ident" 
    [5] => Expires: Sun, 14 Feb 2016 13:04:11 GMT 
    [6] => Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT 
    [7] => Server: ECS (cpm/F9D5) 
    [8] => Vary: Accept-Encoding 
    [9] => X-Cache: HIT 
    [10] => x-ec-custom-error: 1 
    [11] => Content-Length: 1270 
    [12] => Connection: close 
) 

HTTP/1.0 200 OK 
相關問題