-1

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

Peter Vogt
  • 353
  • 1
  • 4
  • 16
  • 1
    `get_headers()` returns either `FALSE` or an array, not a string. What exactly do you want to compare? – Aioros Jan 16 '19 at 00:16
  • But what if the answer is empty? In other words ALL replies that are NOT an array are automatically FALSE? – Peter Vogt Jan 16 '19 at 00:25
  • You are right in a way, it's technically possible for the result to be an empty array (not an empty string), and that would also be "falsy". But if you use `===` for your comparison, as you suggested yourself, that won't matter, so you should be ok. – Aioros Jan 16 '19 at 00:29
  • No it wont because if the result is empty checking with get_headers() === false is TRUE. There must be a way to distinguish between error and no answer? – Peter Vogt Jan 16 '19 at 00:33
  • If the result is empty (an empty array), then `get_headers() === false` is `FALSE`. Instead, `get_headers() == false` is `TRUE`. The strict comparison operator (`===`) does not allow type conversions. – Aioros Jan 16 '19 at 00:37
  • Sorry does not work. Say a call with get_headers() to:http://www.all-seasons-hotels.com/ produces no header not even an empty array and var_dump shows:(false) – Peter Vogt Jan 16 '19 at 00:59
  • are you saying you want the same error if FALSE or no actual data is returned? –  Jan 16 '19 at 01:05
  • Peter, there are two possible cases, either `get_headers` return `false` because there was an error, or it returns an array with available headers, which could be empty. You can distinguish between these two cases using `if (get_headers() === false)`, which will **only** be true if there was an error, and will instead be false if it's an empty array. – Aioros Jan 16 '19 at 01:57

1 Answers1

0

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';
}
Se Kh
  • 37
  • 2
  • All good now. I've discovered that PHP had an extension "openssl" not enabled and because I @get_headers() The error didn't appear anywhere.but always returned FALSE Now it works as expected :-) – Peter Vogt Jan 17 '19 at 04:29