PHP5: dns_get_record () only returns records for subdomains

Running PHP 5.2 on Linux, I am running a series of requests with dns_get_record()

. It can be seen from the documentation that this function replicates dig

, but I see inconsistencies. My goal is to get primary and secondary nameservers that are authoritative for the domains that each hostname belongs to.

dns_get_record('', DNS_NS);

returns good results. dns_get_record('', DNS_NS);

returns nothing. dns_get_record('', DNS_ANY);

only returns the A record. dns_get_record('', DNS_SOA);

returns nothing.

However, from the command line using dig, I can always get at least SOA:

dig NS

or dig SOA

return a valid IN SOA containing the nameservers.

How can I replicate this in PHP? I tried the PEAR Net_DNS module and saw similar shenanigans with dns_get_record()



source to share

2 answers

When you are dig

from the command line, you are much more direct (relatively) connecting to DNS. The result is also formatted for this scenario. When the php function is executed, it returns the values ​​that the authors think will be needed. Does not necessarily correspond directly dig


If you want to get the result dig

, you can use exec('dig ns');

and parse that return the output as you want.

Also, if you query the DNS for a subdomain i.e. www. and not tld (, then you are not going to get any other records associated with it - you are ONLY going to get results associated with this subdomain, which in your case is just the A record.



I had a similar problem: for a host (or subdomain), you need to be more specific and know the text of the DNS record you are requesting.

for example, a DMARC DNS record is a TXT record, as well as SPF and DKIM.

$showResult = dns_get_record('', DNS_TXT); //returns an array


works great.



All Articles