0

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.

Community
  • 1
  • 1
Nick Pickering
  • 3,095
  • 3
  • 29
  • 50

1 Answers1

1

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.

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • Both of your results show the opposite of what I was expecting. `dns_get_record('domain.com', DNS_CNAME);` should return a list of CNAME records for that domain, including a.domain.com, b.domain.com, and www.domain.com. Basically all you were able to accomplish with both `dns_get_record` and `dig` was confirm that the URL you entered was a CNAME record and needed to be pointed to an A record. Previously, `dns_get_record` worked as I expected when passing DNS_CNAME as the argument - and it still works like this with DNS_MX and DNS_NS. – Nick Pickering Jan 24 '13 at 17:44
  • there is no way to get a list of all subdomains for a given server. `NS` lists the nameservers for the given domain, `MX` lists the mail servers for the given domain. `A` gives you the IP for the domain, `CNAME` gives you the CNAME "parent" if the domain has one. I think you misunderstand what DNS allows and what not. – cweiske Jan 24 '13 at 19:03
  • Just imagine what would happen if you asked ".com" for their CNAMEs or their As - and they'd give you all subdomains of .com. – cweiske Jan 24 '13 at 19:05
  • I guess that makes sense, but I swear it worked before - it seemed like it was returning all of the CName records in the zone that contained the A record. Is there a way I can take a url like `domain.com` and get all of the records from its zone file? – Nick Pickering Jan 24 '13 at 19:40
  • 1
    Not in a normal way without using AXFR. See http://stackoverflow.com/q/131989/282601 – cweiske Jan 24 '13 at 20:24
  • OK, thanks. I see everything a little better now. - My next problem is since my question basically has no correct answer, should I accept your answer, or what? – Nick Pickering Jan 24 '13 at 20:30
  • 1
    The question you asked had an answer; I gave it. It wasn't the question you meant to ask, though :) Thanks for accepting. – cweiske Jan 24 '13 at 20:46