dns_get_record

(PHP 5, PHP 7, PHP 8)

dns_get_recordFetch DNS Resource Records associated with a hostname

Descrição

dns_get_record(
    string $hostname,
    int $type = DNS_ANY,
    array &$authoritative_name_servers = null,
    array &$additional_records = null,
    bool $raw = false
): array|false

Fetch DNS Resource Records associated with the given hostname.

Parâmetros

hostname

hostname should 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.

Nota:

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 optional type 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 or DNS_ANY.

Nota:

Because of eccentricities in the performance of libresolv between platforms, DNS_ANY will not always return every record, the slower DNS_ALL will collect all records more reliably.

Nota:

Windows: DNS_CAA is not supported. Support for DNS_A6 is not implemented.

authoritative_name_servers

Passed by reference and, if given, will be populated with Resource Records for the Authoritative Name Servers.

additional_records

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 (the DNS_* constants cannot be used). The return value will contain a data key, which needs to be manually parsed.

Valor Retornado

This function returns an array of associative arrays, ou false em caso de falha. Each associative array contains at minimum the following keys:

Basic DNS attributes
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.

Other keys in associative arrays dependant on 'type'
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.
CAA flags: A one-byte bitfield; currently only bit 0 is defined, meaning 'critical'; other bits are reserved and should be ignored. tag: The CAA tag name (alphanumeric ASCII string). value: The CAA tag value (binary string, may use subformats). For additional information see: » RFC 6844
SOA mname: FQDN of the machine from which the resource records originated. rname: Email address of the administrative contact 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 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.

Registro de Alterações

Versão Descrição
7.0.16, 7.1.2 Added support for CAA record type.

Exemplos

Exemplo #1 Using dns_get_record()

<?php
$result
= dns_get_record("php.net");
print_r($result);
?>

O exemplo acima produzirá algo semelhante a:

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
        )

)

Exemplo #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 additional_records which contains associate records. authoritative_name_servers 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);
?>

O exemplo acima produzirá algo semelhante a:

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
        )

)

Veja Também

add a note add a note

User Contributed Notes 8 notes

up
23
tobias at herkula dot info
8 years ago
This method has no error handling, it simply puts out "false" and it is impossible to check for NXDOMAIN, SERVFAIL, TIMEOUT or any other error...
up
12
dylan at pow7 dot com
15 years ago
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);
?>
up
10
PHP Joe
10 years ago
Although this works very well for general DNS queries if you want to do a direct DNS query to a specified DNS server (rather than using OS resolution) try PHPDNS: http://www.purplepixie.org/phpdns/

You can do direct (TCP or UDP) low-level queries to a nameserver and recurse at will. Very useful for testing specific servers and also for walking through a recursive resolution.
up
6
NaturalBornCamper
6 years ago
You might have the same problem as me, where testing a non-existent domain will search for a subdomain relative to the domain you are executing from, for example:

// Test with working domain
var_dump( dns_get_record('google.com', DNS_A) );
/* works, returns
Array
(
    [host] => google.com
    [class] => IN
    [ttl] => 299
    [type] => A
    [ip] => 172.217.12.142
)
*/

// Test with invalid domain on our website (example.com)
var_dump( dns_get_record('invalidtestingname.com', DNS_A) );
/* Doesn't work, pretend it's a subdomain
Array
(
    [host] => invalidtestingname.com.example.com
    [class] => IN
    [ttl] => 299
    [type] => A
    [ip] => xxx.xxx.xxx.xxx
)
*/

If anyone has that problem, add a "dot" at the end of the domain name, for example, instead of
dns_get_record('invalidtestingname.com', DNS_A);
Do this:
dns_get_record('invalidtestingname.com.', DNS_A);
up
1
heinjan at eendrachtstraat dot nl
7 years ago
Please note that Firewalls and anti malware software detects  (and depending on company policies even blocks) DNS_ANY requests.
In that case the usage of this function fails.
This is because DNS_ANY requests can be  exploited for creating "amplification (D)DOS attackes": You send 1 DNS_ANY request and get a huge amount of information back, thus even small requests can result into hugh network load.

I advise to use a more explicit name-request instead of using DNS_ANY.
up
1
ohcc at 163 dot com
6 years ago
When I use DNS_ALL as the second parameter to invoke dns_get_record() on the OS of Windows, PHP emits a warning with the message "Warning: dns_get_record(): Type '251721779' not supported in blah.php on line blah", and DNS_ANY is always OKAY.
up
-11
karl at influ dot io
10 years ago
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
        )

)
up
-8
Emil
6 years ago
Hi,
If I added domain to directadmin where is script with dns_get_record it gives me local values even if that domain is not really delegated yet to this server.. why?
To Top