Peter Vogt
Peter Vogt

Reputation: 373

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

Upvotes: -1

Views: 724

Answers (1)

Se Kh
Se Kh

Reputation: 47

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';
}

Upvotes: 1

Related Questions