Fetch DNS Resource Records associated with a hostname
dns_get_record ( string $hostname [, int $type = DNS_ANY [, array &$authns [, array &$addtl [, bool $raw = FALSE ]]]] ) : array
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.
Note: Per DNS standards, email addresses are given in user.host format (for example: hostmaster.example.com as opposed to email@example.com), be sure to check this value and modify if necessary before using it with a functions such as mail().
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.
Note: 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.
Passed by reference and, if given, will be populated with Resource Records for the Authoritative Name Servers.
Passed by reference and, if given, will be populated with any Additional Records.
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.
This function returns an array of associative arrays, or FALSE on failure. Each associative array contains at minimum the following keys:
|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.|
|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.|
<? $hostname = "php.net"; $return = dns_get_record($hostname); print_r($return); ?>
Array (  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => AAAA [ipv6] => 2a02:cb40:200::1ad )  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => TXT [txt] => v=spf1 ip4:188.8.131.52 ip6:2a02:cb41::8 ip4:184.108.40.206 ip4:220.127.116.11 ip4:18.104.22.168 ip4:22.214.171.124 ip6:2604:a880:400:d0::1c74:1001 ip6:2a02:cb43:8000::1102 ?all [entries] => Array (  => v=spf1 ip4:126.96.36.199 ip6:2a02:cb41::8 ip4:188.8.131.52 ip4:184.108.40.206 ip4:220.127.116.11 ip4:18.104.22.168 ip6:2604:a880:400:d0::1c74:1001 ip6:2a02:cb43:8000::1102 ?all ) )  => Array ( [host] => php.net [class] => IN [ttl] => 30 [type] => MX [pri] => 0 [target] => php-smtp4-ip4.php.net )  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => SOA [mname] => ns1.php.net [rname] => admin.easydns.com [serial] => 1624244462 [refresh] => 16384 [retry] => 2048 [expire] => 1048576 [minimum-ttl] => 2560 )  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => A [ip] => 22.214.171.124 )  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns4.easydns.info )  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns3.easydns.org )  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns2.easydns.net )  => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns1.easydns.com ) )
<? $hostname = "php.net"; $type = DNS_A; $return = dns_get_record($hostname, $type); print_r($return); ?>
Array (  => Array ( [host] => php.net [class] => IN [ttl] => 3 [type] => A [ip] => 126.96.36.199 ) )
© 2021 Osbo Design