PHP Function Reference

PHP dns_get_record() Function



The PHP dns_get_record() function fetches DNS Resource Records associated with the given hostname.

Syntax

dns_get_record(hostname, type, authns, addtl, raw)

Parameters

hostname Required. Specify a hostname (like "www.alphacodingskills.com").
type Optional. Specify the resource record type to search for. It can 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
  • DNS_ANY
authns Optional. Passed by reference and, if given, will be populated with Resource Records for the Authoritative Name Servers.
addtl Optional. Passed by reference and, if given, will be populated with any Additional Records.
raw Optional. If set to true, the type will be interpreted as a raw DNS type ID. The return value will contain a data key, which needs to be manually parsed. Default is false.

Return Value

Returns an array of associative arrays, or false on failure. Each associative array contains at minimum the following keys:

  • host - The hostname.
  • class - Only returns Internet class records and always return IN.
  • type - The record type. Additional attributes in the arrays depends on the type parameter.
  • ttl - "Time To Live" remaining for this record (original ttl minus the length of time has passed since the authoritative name server was queried).

Additional keys in the arrays depends on the type parameter.

Example: dns_get_record() example

The example below shows the usage of dns_get_record() function.

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

The output of the above code will be:

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

    [1] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 21600
            [type] => NS
            [target] => ns-cloud-d3.googledomains.com
        )

    [2] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 21600
            [type] => NS
            [target] => ns-cloud-d2.googledomains.com
        )

    [3] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 21600
            [type] => NS
            [target] => ns-cloud-d4.googledomains.com
        )

    [4] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 21600
            [type] => NS
            [target] => ns-cloud-d1.googledomains.com
        )

    [5] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 21600
            [type] => SOA
            [mname] => ns-cloud-d1.googledomains.com
            [rname] => cloud-dns-hostmaster.google.com
            [serial] => 1
            [refresh] => 21600
            [retry] => 3600
            [expire] => 259200
            [minimum-ttl] => 300
        )

    [6] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 300
            [type] => MX
            [pri] => 3
            [target] => alt2.aspmx.l.google.com
        )

    [7] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 300
            [type] => MX
            [pri] => 1
            [target] => aspmx.l.google.com
        )

    [8] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 300
            [type] => MX
            [pri] => 4
            [target] => alt3.aspmx.l.google.com
        )

    [9] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 300
            [type] => MX
            [pri] => 5
            [target] => alt4.aspmx.l.google.com
        )

    [10] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 300
            [type] => MX
            [pri] => 2
            [target] => alt1.aspmx.l.google.com
        )

    [11] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 300
            [type] => TXT
            [txt] => google-site-verification=bXjWeKV2-9EyhXk9b9R7SdtE4iOict2Q4fUPDuKa8B0
            [entries] => Array
                (
                    [0] => google-site-verification=bXjWeKV2-9EyhXk9b9R7SdtE4iOict2Q4fUPDuKa8B0
                )

        )

)

Example: using type parameter

Consider one more example which illustrates on using type parameter with this function.

<?php
$result = dns_get_record("alphacodingskills.com", DNS_MX);
print_r($result);
?>

The output of the above code will be:

Array
(
    [0] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 170
            [type] => MX
            [pri] => 1
            [target] => aspmx.l.google.com
        )

    [1] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 170
            [type] => MX
            [pri] => 3
            [target] => alt2.aspmx.l.google.com
        )

    [2] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 170
            [type] => MX
            [pri] => 5
            [target] => alt4.aspmx.l.google.com
        )

    [3] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 170
            [type] => MX
            [pri] => 2
            [target] => alt1.aspmx.l.google.com
        )

    [4] => Array
        (
            [host] => alphacodingskills.com
            [class] => IN
            [ttl] => 170
            [type] => MX
            [pri] => 4
            [target] => alt3.aspmx.l.google.com
        )

)

❮ PHP Network Reference