Why does dns_get_record() PHP function returns empty array?

2.3k Views Asked by At

I had a script in one of my websites that fetched DNS Records using dns_get_record() PHP function. But it's about 2 week that this function returns an empty array. I wrote a test script like below:

<?php
$result = dns_get_record("php.net");
if (count($result)) {
    print_r($result);
} else {
    echo 'Empty Array';
}
?>

It works well on my local machine, but when I test it on my server, it returns Empty Array as result.
What's the matter friends ?

3

There are 3 best solutions below

0
On

Clients need dns and dhcp or hard coded fill in the blanks.

Servers often sit blind to anything just responding to their ip addr directed traffic

1
On

Check DNS ports are open on the server, it requires TCP/UDP port 53.

You can check that your server can make outbound DNS queries by typing the following

dig exmaple.com and see if you get the expected response.

Check server has DNS servers set and then check the firewall as previously suggested.

Lastly, worth check PHP versions on each machine?

0
On

Had similar issue when project was running inside docker container.

To fix this we used Net_DNS2 instead of native dns_get_record, with direct specification of which DNS server should be used.

$dnsResolver = new Net_DNS2_Resolver([
    'nameservers' => ['8.8.8.8']
]);
$dnsResolver->query('php.net', 'NS');