Search code examples
phpdnspear

Get Cname records with NetDNS or get_dns_record


I'm trying get a list of CNAME records using PHP.

This method used to work when I was hosting the code on my old Windows 7 machine, but I moved it to an Ubuntu machine (similar AMP stack setup), and it no longer returned CNAME records for the $url. I uploaded the code to a Windows server and still yet, no CNAME records.

$records['cname'] = dns_get_record($url, DNS_CNAME);

Not that I'm claiming the OS has much to do with anything, maybe I had something set up a certain way on my old machine that allowed this to work. It currently returns a simple empty array, where as it used to return a list of CNAME records that matched up with what was in my DNS management service. I've read the article here:

Why wouldn't dns_get_record show CNAMEs when I KNOW they exist?

and ended up trying to implement the functionality with Pear's NetDns2 Library, with the following code. And still, the answer array of the response is empty. Not sure if the problem is the same from get_dns_record to NetDNS2 or if I'm doing something wrong in both cases, but the sites tested surely have CNAME records, including google.com and yahoo.com.

//using Google Name Server
$rslvr = new Net_DNS2_Resolver(array('nameservers' => array('8.8.8.8')));
$records['cname'] = $rslvr->query($url, 'CNAME');

Any help is appreciated, the end goal is to be able to obtain a list of CNAME records like I was on my old machine.


Solution

  • Works here with PHP 5.4.9.

    The first record (cweiske.de) is no CNAME but directly points to an IP address:

    php > var_dump(dns_get_record('cweiske.de', DNS_CNAME));
    array(0) {
    }
    

    The second is actually a CNAME to the first record:

    php > var_dump(dns_get_record('www.cweiske.de', DNS_CNAME));
    array(1) {
      [0] =>
      array(5) {
        'host' =>
        string(14) "www.cweiske.de"
        'class' =>
        string(2) "IN"
        'ttl' =>
        int(86400)
        'type' =>
        string(5) "CNAME"
        'target' =>
        string(10) "cweiske.de"
      }
    }
    

    dig shows the same:

    $ dig cweiske.de
    cweiske.de.     577 IN  A   83.169.7.198
    
    $ dig www.cweiske.de
    www.cweiske.de.     86397   IN  CNAME   cweiske.de.
    cweiske.de.     597 IN  A   83.169.7.198
    

    To check if it's your or PHP's problem, use dig or nslookup to test.