1

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...

<?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. 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.

1 Answer 1

1

Please use get_headers() function it will return you status code, refer php docs -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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.