2

Consider the following:

$url = 'http://psyng.com/u/9716602b';
$headers = get_headers($url, 1);
print_r($headers);

Since the domain psyng.com is unresolvable, this code results in:

Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: 
No such host is known

And then the script stops running. Is there a way to keep the rest of the script running - in other words: to catch the error, and move forward with resolving the next URL? So something like:

$url = 'http://psyng.com/u/9716602b';
$headers = get_headers($url, 1);
if ($headers == 'No such host is known') {
  // nevermind, just move on to the next URL in the list...
}
else {
  // resolve header stuff...
}
Pr0no
  • 4,479
  • 21
  • 69
  • 111
  • get_headers Inconsistency : http://stackoverflow.com/questions/12781795/get-headers-inconsistency – Baba Oct 08 '12 at 15:39

2 Answers2

3

The script shouldn't stop running as the message produced is just a warning. I tested this script myself and that's the behaviour I see. You can see in the documentation that get_headers() will return FALSE on failure so your condition should actually be

if ($headers === FALSE) {
    // nevermind, just move on to the next URL in the list...
Michael Mior
  • 27,152
  • 8
  • 85
  • 111
0

The function get_headers returns a boolean result; the purpose of print_r is to return boolean in a human readable format.

<?php
$url = 'http://psyng.com/u/9716602b';
$headers = get_headers($url, 1);
if ($headers === FALSE) { //Test for a boolean result.
  // nevermind, just move on to the next URL in the list...
}
else {
  // resolve header stuff...
}
?>
mjsa
  • 4,003
  • 1
  • 24
  • 35