PHP get DNS Nameserver , Cname , MX , A records of a domain

PHP get DNS Nameserver , Cname , MX , A records of a domain

 
 

 

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.

Nameserver - NS Records :

$ php -a
Interactive shell

php > print_r(dns_get_record('www.binarytides.com' , DNS_NS));
Array
(
    [0] => Array
        (
            [host] => binarytides.com
            [type] => NS
            [target] => omikro1.allwebserver.com
            [class] => IN
            [ttl] => 86140
        )

    [1] => Array
        (
            [host] => binarytides.com
            [type] => NS
            [target] => omikro2.allwebserver.com
            [class] => IN
            [ttl] => 86140
        )

)
php > 

A Record - IP address :

php > print_r(dns_get_record('www.binarytides.com' , DNS_A));
Array
(
    [0] => Array
        (
            [host] => binarytides.com
            [type] => A
            [ip] => 64.131.72.23
            [class] => IN
            [ttl] => 14046
        )

)
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('www.binarytides.com'));
64.131.72.23
php >

Another method gethostbynamel would give an array of IPs :

php > print_r(gethostbynamel('www.google.com'));
Array
(
    [0] => 74.125.71.147
    [1] => 74.125.71.99
    [2] => 74.125.71.104
    [3] => 74.125.71.106
    [4] => 74.125.71.105
    [5] => 74.125.71.103
)
php > 

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('www.binarytides.com' , DNS_MX));
Array
(
    [0] => Array
        (
            [host] => binarytides.com
            [type] => MX
            [pri] => 0
            [target] => binarytides.com
            [class] => IN
            [ttl] => 14400
        )

)
php > 

For MX records there is an alternative function called getmxrr , which can be used like this :

php > getmxrr('www.binarytides.com' , $mxhosts);
php > print_r($mxhosts);
Array
(
    [0] => binarytides.com
)
php >

It returns an array of mxhosts for a given hostname/domain.

CNAME Records - Canonical Alias :

php > print_r(dns_get_record('www.binarytides.com' , DNS_CNAME));
Array
(
    [0] => Array
        (
            [host] => www.binarytides.com
            [type] => CNAME
            [target] => binarytides.com
            [class] => IN
            [ttl] => 13932
        )

)
php > 

With the DNS_ALL option you can get all the above information at once :

php > print_r(dns_get_record('www.binarytides.com' , DNS_ALL));
Array
(
    [0] => Array
        (
            [host] => binarytides.com
            [type] => A
            [ip] => 64.131.72.23
            [class] => IN
            [ttl] => 14202
        )

    [1] => Array
        (
            [host] => binarytides.com
            [type] => NS
            [target] => omikro2.allwebserver.com
            [class] => IN
            [ttl] => 86344
        )

    [2] => Array
        (
            [host] => binarytides.com
            [type] => NS
            [target] => omikro1.allwebserver.com
            [class] => IN
            [ttl] => 86344
        )

    [3] => Array
        (
            [host] => www.binarytides.com
            [type] => CNAME
            [target] => binarytides.com
            [class] => IN
            [ttl] => 13800
        )

    [4] => Array
        (
            [host] => binarytides.com
            [type] => SOA
            [mname] => host1.allwebserver.com
            [rname] => omikrosys.gmail.com
            [serial] => 2011020202
            [refresh] => 86400
            [retry] => 7200
            [expire] => 3600000
            [minimum-ttl] => 86400
            [class] => IN
            [ttl] => 86344
        )

    [5] => Array
        (
            [host] => binarytides.com
            [type] => MX
            [pri] => 0
            [target] => binarytides.com
            [class] => IN
            [ttl] => 14199
        )

)
php > 

Other options available are :
DNS_HINFO, DNS_PTR, DNS_SOA, DNS_TXT, DNS_AAAA, DNS_SRV, DNS_NAPTR, DNS_A6, DNS_ANY.

References :

1. Manual at : http://php.net/manual/en/function.dns-get-record.php

 
 


We use our own cookies and third-party cookies to improve our services, show products based on your preferences, analyse the browsing habits of our users, and enable interaction with social networks. Continuing to browse our sites implies full acceptance of their use. You can change your cookie setting or get more information here: Cookies policy .