phperror-checkingget-headers

PHP get_headers() result distinguish


I use the function get_headers( $url).

If according to http://php.net/manual/en/language.types.boolean.php an e,pty string equates to false.

How can I distinguish between the function returning get_headers( $url) === false and a empty string?

Or in other words how can I distinguish between the error case and good case?

Thanks


Solution

  • If you want check HTTP status, use this:

    <?php
    $headers = get_headers($url);
    if($headers !== FALSE AND $headers[0] === 'HTTP/1.0 200 OK'){
        print_r($headers);
    }else{
        echo 'Error';
    }
    

    Check for any HTTP status:

    <?php
    $headers = get_headers($url);
    if($headers !== FALSE){ // or can use "is_array($headers)"
        print_r($headers);
    }else{
        echo 'Error';
    }