9

I'm querying a whole bunch of addresses, some are online and some are not. I can't seem to get around this error however, even catching the exception fails :(

 dns_get_record(): A temporary server error occurred. 


        try {
            $result = dns_get_record('_minecraft._tcp.' . $addr, DNS_SRV);
        }
        catch (Exception $e) {
            return [$addr,$port];
        }

If this error occurs, I want to continue the script, skipping the record, however currently the script just halts.

Any help appreciated!!

Josh Undefined
  • 1,496
  • 5
  • 16
  • 27

3 Answers3

17

I can't catch this exception too. And how I understood it's a bug of php: https://bugs.php.net/bug.php?id=73149

But I found another solution. You can use @ when you call this function. This symbol kill all errors when you call this one. And it will looks like that:

$dns = @dns_get_record($domain, DNS_A);
if(!$dns){
    return false;
}
  • 1
    It looks like this is the workaround for this PHP bug. While we ended up using an external DNS API (yes i know...) for this issue I'll mark this as the answer for anyone finding this question. Thanks!! – Josh Undefined Jun 07 '17 at 12:37
  • Longer description why https://dustri.org/b/a-short-tale-on-phps-dns_get_record.html – michalzuber Mar 02 '20 at 08:57
0

I was able to get the IP (A record) for a host using the below PHP function

gethostbynamel(string $hostname): array|false

Reference: gethostbynamel — Get a list of IPv4 addresses corresponding to a given Internet host name

Senthil
  • 171
  • 4
-6

try this:

     try {
         $dns = dns_get_record($domain, DNS_A);
     }
     catch (Exception $e) {
         if ($e->getMessage() !== 'dns_get_record(): A temporary server error     occurred.') {
             throw $e;
         }
         $dns = false;
     }