1. php
  2. /references
  3. /dns-get-record

PHP dns_get_record() Function

PHP's dns_get_record() retrieves DNS resource records for a given domain name. For example, if you wanted to fetch all DNS records fort a given domain:

<?php
$records = dns_get_record('webreference.com', DNS_ALL);

print_r($records)
?>

Here is a sample result:

Array
(
    [0] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => A
            [ip] => 104.21.4.229
        )

    [1] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => A
            [ip] => 172.67.132.147
        )

    [2] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 21600
            [type] => NS
            [target] => dina.ns.cloudflare.com
        )

    [3] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 21600
            [type] => NS
            [target] => roan.ns.cloudflare.com
        )

    [4] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 1800
            [type] => SOA
            [mname] => dina.ns.cloudflare.com
            [rname] => dns.cloudflare.com
            [serial] => 2336434328
            [refresh] => 10000
            [retry] => 2400
            [expire] => 604800
            [minimum-ttl] => 1800
        )

    [5] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => MX
            [pri] => 10
            [target] => mail.protonmail.ch
        )

    [6] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => MX
            [pri] => 20
            [target] => mailsec.protonmail.ch
        )

    [7] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => TXT
            [txt] => google-site-verification=9NYwPFDWexFV9mrjo51M9NKp-AAYMfAlmB_MfgIhNxo
            [entries] => Array
                (
                    [0] => google-site-verification=9NYwPFDWexFV9mrjo51M9NKp-AAYMfAlmB_MfgIhNxo
                )

        )

    [8] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => TXT
            [txt] => protonmail-verification=c6fdafc8cfe9557f354d175f8d02f82a73cdf122
            [entries] => Array
                (
                    [0] => protonmail-verification=c6fdafc8cfe9557f354d175f8d02f82a73cdf122
                )

        )

    [9] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => TXT
            [txt] => v=spf1 include:_spf.protonmail.ch mx ~all
            [entries] => Array
                (
                    [0] => v=spf1 include:_spf.protonmail.ch mx ~all
                )

        )

    [10] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => AAAA
            [ipv6] => 2606:4700:3033::ac43:8493
        )

    [11] => Array
        (
            [host] => webreference.com
            [class] => IN
            [ttl] => 300
            [type] => AAAA
            [ipv6] => 2606:4700:3033::6815:4e5
        )

)

Syntax and Parameters

array dns_get_record ( string $hostname [, int $type = DNS_ANY [, array &$authns [, array &$addtl [, bool $raw = false ]]]] )
ParameterDescriptionOptional/Required
$hostnameThe domain name to look up.Required
$typeThe type of record to look up.Optional
$authnsAn array of authority nameserver records.Optional
$addtlAn array of additional records.Optional
$rawIf true, return the raw data.Optional

The function will return an array of DNS resource records matching the specified domain name and record type. Each element in the array represents a single resource record, and the array is sorted in the order the records are returned by the DNS server.

The $type parameter can be any of the following values:

ConstantDescription
DNS_AIPv4 addresses
DNS_AAAAIPv6 addresses
DNS_CNAMECanonical name records
DNS_MXMail exchange records
DNS_NSName server records
DNS_PTRPointer records
DNS_SOAStart of authority records
DNS_TXTText records
DNS_HINFOHost information records
DNS_SRVService records
DNS_NAPTRNaming authority pointer records
DNS_A6IPv6 address records (deprecated)
DNS_ANY (Default)Look up all types of records
DNS_ALLLook up all types of records

There is no functional difference between DNS_ANY and DNS_ALL except for some eccentricities in how they return results. DNS_ALL is slower, but more reliable.

$authns can be useful in a variety of contexts, such as:

  • Debugging DNS issues: If you are experiencing problems with a domain's DNS records, you may want to check the authority nameserver records to ensure that they are correct.

  • Analyzing DNS configurations: By looking at a domain's authority nameserver records, you can get a sense of how the domain's DNS is set up and whether it is using any third-party DNS services.

The $addtl parameter can be used to fetch DNS resource records that are not typically used in the resolution of domain names, but that may contain useful information. You can use it to check for the presence of DNSSEC records or to gather information about a domain's SSH or TLS configuration.

  • DS records: Delegation signer records, which are used to secure a domain's DNS records with DNSSEC.

  • SSHFP records: SSH fingerprint records, which are used to associate a domain with a particular SSH host key.

  • TLSA records: Transport Layer Security Association records, which are used to associate a domain with a particular TLS certificate.

$raw when set to true will do exactly what it says and return the raw result of the DNS query.

Additional examples

Look up MX records for a domain

<?php
$records = dns_get_record('webreference.com', DNS_MX);

foreach ($records as $record) {
    echo $record['target'] . "\n";
}
?>

Look up the A record for a domain

<?php
$records = dns_get_record('webreference.com', DNS_A);

print_r($records);

?>

gethostbyname()

gethostbynamel()

gethostbyaddr()

checkdnsrr()