PHP获取相同请求的响应时间和http状态代码

PHP获取相同请求的响应时间和http状态代码,php,curl,Php,Curl,我可以使用Curl获取URL的HTTP状态代码,我可以通过执行以下操作获取URL的响应时间 <?php // check responsetime for a webbserver function pingDomain($domain){ $starttime = microtime(true); // supress error messages with @ $file = @fsockopen($domain, 80, $errno, $err

我可以使用Curl获取URL的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状态码和响应时间。如果这只能通过卷曲来实现,那就太好了

注意:我不想/不需要URL中的任何其他信息,因为这会减慢我的进程。

请使用get_headers()函数,它将返回您的状态代码,请参阅php文档-


输出-->
排列
(
[0]=>HTTP/1.0 200正常
[1] =>缓存控制:最大年龄=604800
[2] =>内容类型:文本/html
[3] =>日期:太阳,2016年2月7日13:04:11 GMT
[4] =>Etag:“359670651+gzip+ident”
[5] =>到期时间:2016年2月14日星期日13:04:11 GMT
[6] =>上次修改时间:2013年8月9日星期五23:54:35 GMT
[7] =>服务器:ECS(cpm/F9D5)
[8] =>变化:接受编码
[9] =>X缓存:命中
[10] =>x-ec-custom-error:1
[11] =>内容长度:1270
[12] =>连接:关闭
)
HTTP/1.0 200正常
<?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