get_headers Inconsistency [closed]
Solution 1:
The problem is nothing to do with the length of the domain name, it is simply whether the domain exists.
You are using a DNS service that resolves non-existent domains to a server that gives you a "friendly" error page, which it returns with a 200 response code. This means it is also not a problem with get_headers()
specifically, it is any procedure with an underlying reliance on sensible DNS lookups.
A way to handle this without hardcoding a work around for every environment you work in might look something like this:
// A domain that definitely does not exist. The easiest way to guarantee that
// this continues to work is to use an illegal top-level domain (TLD) suffix
$testDomain = 'idontexist.tld';
// If this resolves to an IP, we know that we are behind a service such as this
// We can simply compare the actual domain we test with the result of this
$badIP = gethostbyname($testDomain);
// Then when you want to get_headers()
$url = 'http://www.domainnnnnnnnnnnnnnnnnnnnnnnnnnnn.com/CraxyFile.jpg';
$host = parse_url($url, PHP_URL_HOST);
if (gethostbyname($host) === $badIP) {
// The domain does not exist - probably handle this as if it were a 404
} else {
// do the actual get_headers() stuff here
}
You may want to somehow cache the return value of the first call to gethostbyname()
, since you know you are looking up a name that does not exist, and this can often take a few seconds.