PHP has a useful function called dns_get_record that can be used to get different types of records for a give domain , very easily.
These include the nameserver records, A records, CNAME records, MX records, SOA records etc.
The following examples are using the interactive shell, but you would use the function the same way in your php scripts.
1. Get Nameserver - NS Records :
The following example gets the nameserver records of a given domain name.
$ php -a Interactive shell php > print_r( dns_get_record('reddit.com' , DNS_NS) ); Array ( [0] => Array ( [host] => reddit.com [class] => IN [ttl] => 21599 [type] => NS [target] => ns-1029.awsdns-00.org ) [1] => Array ( [host] => reddit.com [class] => IN [ttl] => 21599 [type] => NS [target] => ns-1887.awsdns-43.co.uk ) [2] => Array ( [host] => reddit.com [class] => IN [ttl] => 21599 [type] => NS [target] => ns-378.awsdns-47.com ) [3] => Array ( [host] => reddit.com [class] => IN [ttl] => 21599 [type] => NS [target] => ns-557.awsdns-05.net ) ) php >
2. A Record - IP address
The A record gives the ip address of the server where you would find the http server or other web services being hosted.
php > print_r( dns_get_record('reddit.com' , DNS_A) ); Array ( [0] => Array ( [host] => reddit.com [class] => IN [ttl] => 252 [type] => A [ip] => 151.101.193.140 ) [1] => Array ( [host] => reddit.com [class] => IN [ttl] => 252 [type] => A [ip] => 151.101.1.140 ) [2] => Array ( [host] => reddit.com [class] => IN [ttl] => 252 [type] => A [ip] => 151.101.65.140 ) [3] => Array ( [host] => reddit.com [class] => IN [ttl] => 252 [type] => A [ip] => 151.101.129.140 ) ) php >
Another simple method to get the A record or IP address of a domain name is to use the function gethostbyname :
php > print_r( gethostbyname('reddit.com') ); 151.101.129.140 php >
The above method returns only 1 ip address. If you want all the ip addresses then use the gethostbynamel method instead, which returns an array of ip addresses.
php > print_r( gethostbynamel('reddit.com') ); Array ( [0] => 151.101.129.140 [1] => 151.101.65.140 [2] => 151.101.1.140 [3] => 151.101.193.140 ) php >
3. MX Record - Mail Server
MX Records give the mail server for a given domain. These mail servers can be used to send mails to a certain email on that domain say [email protected]
php > print_r( dns_get_record('reddit.com' , DNS_MX) ); Array ( [0] => Array ( [host] => reddit.com [class] => IN [ttl] => 299 [type] => MX [pri] => 1 [target] => aspmx.l.google.com ) [1] => Array ( [host] => reddit.com [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => aspmx2.googlemail.com ) [2] => Array ( [host] => reddit.com [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => aspmx3.googlemail.com ) [3] => Array ( [host] => reddit.com [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt1.aspmx.l.google.com ) [4] => Array ( [host] => reddit.com [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt2.aspmx.l.google.com ) ) php >
For MX records there is an alternative function called getmxrr , which can be used like this :
php > getmxrr('reddit.com', $mxhosts); php > print_r($mxhosts); Array ( [0] => alt2.aspmx.l.google.com [1] => alt1.aspmx.l.google.com [2] => aspmx3.googlemail.com [3] => aspmx2.googlemail.com [4] => aspmx.l.google.com ) php >
It returns an array of mxhosts for a given hostname/domain.
4. CNAME Records - Canonical Alias
php > print_r( dns_get_record('www.reddit.com' , DNS_CNAME) ); Array ( [0] => Array ( [host] => www.reddit.com [class] => IN [ttl] => 281 [type] => CNAME [target] => reddit.map.fastly.net ) ) php >
Conclusion
The dns_get_record method supports more options that provide dns records of other types.
Other options available include:
DNS_HINFO DNS_PTR DNS_SOA DNS_TXT DNS_AAAA DNS_SRV DNS_NAPTR DNS_A6 DNS_ANY
Links and References :
1. Manual at : http://php.net/manual/en/function.dns-get-record.php
Can use package `mesour/dns-checker` to get records as objects in `DnsRecordSet` and compare DNS records :)
Why this code not work for my page?