Php/docs/function.dns-get-record
dns_get_record
(PHP 5, PHP 7)
dns_get_record — Fetch DNS Resource Records associated with a hostname
Description
dns_get_record
( string $hostname
[, int $type
= DNS_ANY
[, array &$authns
[, array &$addtl
[, bool $raw
= FALSE
]]]] ) : array|false
Fetch DNS Resource Records associated with the given
hostname
.
Parameters
hostname
hostname
should be a valid DNS hostname such as "www.example.com
". Reverse lookups can be generated usingin-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[email protected]
), 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 optionaltype
parameter. May be any one of the following:DNS_A
,DNS_CNAME
,DNS_HINFO
,DNS_CAA
,DNS_MX
,DNS_NS
,DNS_PTR
,DNS_SOA
,DNS_TXT
,DNS_AAAA
,DNS_SRV
,DNS_NAPTR
,DNS_A6
,DNS_ALL
orDNS_ANY
.Note:
Because of eccentricities in the performance of libresolv between platforms,
DNS_ANY
will not always return every record, the slowerDNS_ALL
will collect all records more reliably.Note:
DNS_CAA
is not supported on Windows.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.
raw
The
type
will be interpreted as a raw DNS type ID (theDNS_*
constants cannot be used). The return value will contain adata
key, which needs to be manually parsed.
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 |
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.
|
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.
|
CAA
|
flags : A one-byte bitfield; currently only bit 0 is defined,
meaning 'critical'; other bits are reserved and should be ignored.
|
SOA
|
mname : FQDN of the machine from which the resource
records originated.
|
AAAA
|
ipv6 : IPv6 address
|
A6 (PHP >= 5.1.0)
|
masklen : Length (in bits) to inherit from the target
specified by |
SRV
|
pri : (Priority) lowest priorities should be used first.
|
NAPTR
|
order and pref : Equivalent to
|
Changelog
Version | Description |
---|---|
7.0.16, 7.1.2 | Added support for CAA record type. |
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 ) )