Get more than one type at once like this:
<?php
$dnsr = dns_get_record('php.net', DNS_A + DNS_NS);
print_r($dnsr);
?>
Using DNS_ALL fails on some domains where DNS_ANY works. I noticed the function getting stuck on the DNS_PTR record, which caused it to return FALSE with this error:
PHP Warning: dns_get_record(): res_nsend() failed in ....
This gets all records except DNS_PTR:
<?php
$dnsr = dns_get_record('php.net', DNS_ALL - DNS_PTR);
print_r($dnsr);
?>
dns_get_record
(PHP 5)
dns_get_record — Fetch DNS Resource Records associated with a hostname
Description
$hostname
[, int $type = DNS_ANY
[, array &$authns
[, array &$addtl
]]] )
Fetch DNS Resource Records associated with the given
hostname.
Parameters
-
hostname -
hostnameshould be a valid DNS hostname such as "www.example.com". Reverse lookups can be generated using in-addr.arpa notation, but gethostbyaddr() is more suitable for the majority of reverse lookups.Note:
Per DNS standards, email addresses are given in user.host format (for example: hostmaster.example.com as opposed to hostmaster@example.com), be sure to check this value and modify if necessary before using it with a functions such as mail().
-
type -
By default, dns_get_record() will search for any resource records associated with
hostname. To limit the query, specify the optionaltypeparameter. May be any one of the following:DNS_A,DNS_CNAME,DNS_HINFO,DNS_MX,DNS_NS,DNS_PTR,DNS_SOA,DNS_TXT,DNS_AAAA,DNS_SRV,DNS_NAPTR,DNS_A6,DNS_ALLorDNS_ANY.Note:
Because of eccentricities in the performance of libresolv between platforms,
DNS_ANYwill not always return every record, the slowerDNS_ALLwill collect all records more reliably. -
authns -
Passed by reference and, if given, will be populated with Resource Records for the Authoritative Name Servers.
-
addtl -
Passed by reference and, if given, will be populated with any Additional Records.
Return Values
This function returns an array of associative arrays,
or FALSE on failure. Each associative array contains
at minimum the following keys:
| Attribute | Meaning |
|---|---|
| host | The record in the DNS namespace to which the rest of the associated data refers. |
| class | dns_get_record() only returns Internet class records and as such this parameter will always return IN. |
| type | String containing the record type. Additional attributes will also be contained in the resulting array dependant on the value of type. See table below. |
| ttl | "Time To Live" remaining for this record. This will not equal the record's original ttl, but will rather equal the original ttl minus whatever length of time has passed since the authoritative name server was queried. |
| Type | Extra Columns |
|---|---|
| A | ip: An IPv4 addresses in dotted decimal notation. |
| MX | pri: Priority of mail exchanger. Lower numbers indicate greater priority. target: FQDN of the mail exchanger. See also dns_get_mx(). |
| CNAME | target: FQDN of location in DNS namespace to which the record is aliased. |
| NS | target: FQDN of the name server which is authoritative for this hostname. |
| PTR | target: Location within the DNS namespace to which this record points. |
| TXT | txt: Arbitrary string data associated with this record. |
| HINFO | cpu: IANA number designating the CPU of the machine referenced by this record. os: IANA number designating the Operating System on the machine referenced by this record. See IANA's » Operating System Names for the meaning of these values. |
| SOA | mname: FQDN of the machine from which the resource records originated. rname: Email address of the administrative contain for this domain. serial: Serial # of this revision of the requested domain. refresh: Refresh interval (seconds) secondary name servers should use when updating remote copies of this domain. retry: Length of time (seconds) to wait after a failed refresh before making a second attempt. expire: Maximum length of time (seconds) a secondary DNS server should retain remote copies of the zone data without a successful refresh before discarding. minimum-ttl: Minimum length of time (seconds) a client can continue to use a DNS resolution before it should request a new resolution from the server. Can be overridden by individual resource records. |
| AAAA | ipv6: IPv6 address |
| A6(PHP >= 5.1.0) |
masklen: Length (in bits) to inherit from the target
specified by chain.
ipv6: Address for this specific record to merge with
chain.
chain: Parent record to merge with
ipv6 data.
|
| SRV |
pri: (Priority) lowest priorities should be used first.
weight: Ranking to weight which of commonly prioritized
targets should be chosen at random.
target and port: hostname and port
where the requested service can be found.
For additional information see: » RFC 2782
|
| NAPTR |
order and pref: Equivalent to
pri and weight above.
flags, services, regex,
and replacement: Parameters as defined by
» RFC 2915.
|
Changelog
| Version | Description |
|---|---|
| 5.3.0 | This function is now available on Windows platforms. |
| 5.3.0 |
Prior to this release, if the authns parameter
was given, the addtl parameter was also
required.
|
Examples
Example #1 Using dns_get_record()
<?php
$result = dns_get_record("php.net");
print_r($result);
?>
The above example will output something similar to:
Array
(
[0] => Array
(
[host] => php.net
[type] => MX
[pri] => 5
[target] => pair2.php.net
[class] => IN
[ttl] => 6765
)
[1] => Array
(
[host] => php.net
[type] => A
[ip] => 64.246.30.37
[class] => IN
[ttl] => 8125
)
)
Example #2 Using dns_get_record() and DNS_ANY
Since it's very common to want the IP address of a mail server
once the MX record has been resolved, dns_get_record()
also returns an array in addtl which
contains associate records. authns
is returned as well containing a list of authoritative name
servers.
<?php
/* Request "ANY" record for php.net,
and create $authns and $addtl arrays
containing list of name servers and
any additional records which go with
them */
$result = dns_get_record("php.net", DNS_ANY, $authns, $addtl);
echo "Result = ";
print_r($result);
echo "Auth NS = ";
print_r($authns);
echo "Additional = ";
print_r($addtl);
?>
The above example will output something similar to:
Result = Array
(
[0] => Array
(
[host] => php.net
[type] => MX
[pri] => 5
[target] => pair2.php.net
[class] => IN
[ttl] => 6765
)
[1] => Array
(
[host] => php.net
[type] => A
[ip] => 64.246.30.37
[class] => IN
[ttl] => 8125
)
)
Auth NS = Array
(
[0] => Array
(
[host] => php.net
[type] => NS
[target] => remote1.easydns.com
[class] => IN
[ttl] => 10722
)
[1] => Array
(
[host] => php.net
[type] => NS
[target] => remote2.easydns.com
[class] => IN
[ttl] => 10722
)
[2] => Array
(
[host] => php.net
[type] => NS
[target] => ns1.easydns.com
[class] => IN
[ttl] => 10722
)
[3] => Array
(
[host] => php.net
[type] => NS
[target] => ns2.easydns.com
[class] => IN
[ttl] => 10722
)
)
Additional = Array
(
[0] => Array
(
[host] => pair2.php.net
[type] => A
[ip] => 216.92.131.5
[class] => IN
[ttl] => 6766
)
[1] => Array
(
[host] => remote1.easydns.com
[type] => A
[ip] => 64.39.29.212
[class] => IN
[ttl] => 100384
)
[2] => Array
(
[host] => remote2.easydns.com
[type] => A
[ip] => 212.100.224.80
[class] => IN
[ttl] => 81241
)
[3] => Array
(
[host] => ns1.easydns.com
[type] => A
[ip] => 216.220.40.243
[class] => IN
[ttl] => 81241
)
[4] => Array
(
[host] => ns2.easydns.com
[type] => A
[ip] => 216.220.40.244
[class] => IN
[ttl] => 81241
)
)
I've done a backport of the function to PHP4.
It is available as extension at:
http://nona.net/software/dns_get_record/
comments & feedback appreciated.
--
Alexander Mayrhofer
axelm-php@nona.net
There's a comment below from 7 years ago regarding BSD and MacOSX, I'd just like to follow up incase some people see that and don't think it'll work on MacOSX.
Software:
System Software Overview:
System Version: OS X 10.8.3 (12D78)
Kernel Version: Darwin 12.3.0
Boot Volume: Macintosh HD
Boot Mode: Normal
Computer Name: Karl’s iMac
User Name: Karl Kloppenborg (karl)
Secure Virtual Memory: Enabled
Time since boot: 10 days 20:24
--------------
# php -a
php > print_r(dns_get_record('google.com', DNS_MX));
Array
(
[0] => Array
(
[host] => google.com
[type] => MX
[pri] => 10
[target] => aspmx.l.google.com
[class] => IN
[ttl] => 749
)
[1] => Array
(
[host] => google.com
[type] => MX
[pri] => 30
[target] => alt2.aspmx.l.google.com
[class] => IN
[ttl] => 749
)
[2] => Array
(
[host] => google.com
[type] => MX
[pri] => 50
[target] => alt4.aspmx.l.google.com
[class] => IN
[ttl] => 749
)
[3] => Array
(
[host] => google.com
[type] => MX
[pri] => 40
[target] => alt3.aspmx.l.google.com
[class] => IN
[ttl] => 749
)
[4] => Array
(
[host] => google.com
[type] => MX
[pri] => 20
[target] => alt1.aspmx.l.google.com
[class] => IN
[ttl] => 749
)
)
If only NS records are set (when the domain is not configured on the host), DNS_NS must be used, otherwise an error occurs and it returns nothing!
Its worth nothing that this function returns nothing if the server you are running it from is authoritative for the domain you are checking (common on shared hosting platforms) when looking up 'NS' records
In this instance $authns is an array containing no keys or values:
dns_get_record("alocaldomain.com","NS",$authns,$addtl);
I recently needed to do some name server and IP discovery on the fly.
Server and domain monitoring system. If you've got a system with dig
the below seems to work fine for discovering name server and a
addresses. Shouldn't be hard for you to modify it for the other records if
needed.
$_REQUEST[url] = "http://www.example.com/";
$url_parts = parse_url($_REQUEST[url]);
$dns = Dig($url_parts[host]);
print_r($dns[name_servers]);
print_r($dns[ips]);
function Dig ($domain) {
$dig = `dig $domain`;
preg_match_all("/in\s+ns\s+(.+?)\s+/is"
,$dig,$name_servers,PREG_PATTERN_ORDER);
preg_match_all("/$domain.\s+[0-9]+\s+in\s+a\s+([0-9.]+)\s+/is"
,$dig,$ips,PREG_PATTERN_ORDER);
$dns[name_servers] = $name_servers[1];
$dns[ips] = $ips[1];
return($dns);
}
Note that if you check a non-existing domain you will get:
Warning: dns_get_record() [function.dns-get-record]: res_nsend() failed in ...
and the function will return an empty srtring (not array). I do not see this described above.
The docs say "nor does it (currently) work on *BSD systems". Unfortunately this includes MacOS X. It's just not there in my build of 5.1.2 running on 10.4.4.
