7

According to the docs

get_headers

should return false if a header request fails.

However, it is breaking my code w/ this warning:

Warning: get_headers(http://ideone.com/) [function.get-headers]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in some.php on line 21

I wanted to test that a given url was available using code similar to this:

$res[$i] = (get_headers($temp[$i]) == false);
  • That's also what the first comment says. And the rest of the manual page doesn't contraire that, and it's not exactly unexpected for lower-level errors (here: DNS resolution). Is this a complaint or a question? – mario Jun 17 '13 at 18:42

2 Answers2

10

I don't think this is a doc bug; at least internally, it uses the common implementation for opening streams to some location.

And so this function will throw the same warnings like every other function using the internal php_stream_open_wrapper_ex, when passing a) an invalid address or b) an unreachable address.

If you want to suppress this warning, prepend get_headers with an @:

$res[$i] = (@get_headers($temp[$i]) === false);
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • @OneTrickPony yes. CURL uses the libcurl, which naturally doesn't affect any error handling of PHP. – bwoebi Jun 17 '13 at 18:47
2

You should have a look at the first comment on the php man page of get_headers

It seems like it's working as expected.

So you must either disable warnings, either find a workaround.

After a quick search, I found this post on phpfreaks that might help you. The solution used there is to use gethostbyname first to resolve the IP address of the server, then retrieve the headers if it doesn't return false.

Or more simply, disable the warning with an @ before the call. (See Can I try/catch a warning? and php's error reporting page)

Community
  • 1
  • 1
Jerska
  • 11,722
  • 4
  • 35
  • 54