简体   繁体   English

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

[英]PHP get response time and http status code same request

I can get the HTTP status code of a URL using Curl and I can get the response time of a URL by doing something like the following... 我可以使用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;
}
?>

However, I'm struggling to think of a way to get the HTTP status code and the response time using the same request. 但是,我正在努力思考一种使用同一请求获取HTTP状态代码响应时间的方法。 If this is possible to do only via curl that would be great. 如果只有通过卷曲才能做到这一点,那就太好了。

Note: I don't want/need any other information from the URL as this will slow down my process. 注意:我不需要/不需要URL中的任何其他信息,因为这会减慢我的处理速度。

Please use get_headers() function it will return you status code, refer php docs - http://php.net/manual/en/function.get-headers.php 请使用get_headers()函数,它将返回您的状态码,请参考php文档-http: //php.net/manual/zh/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM