dns_get_record
Description
The dns_get_record of Network for PHP fetch DNS Resource Records associated with a hostname.
Syntax
dns_get_record( string $hostname, int $type = DNS_ANY, array &$authoritative_name_servers = null, array &$additional_records = null, bool $raw = false ): array|false
Parameters
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.
Note: 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.
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.
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.
Return
This function returns an array of associative arrays, or false on failure. Each associative array contains at minimum the following keys:
Attribute | Description |
---|---|
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. |
Type | Description |
---|---|
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. |
Examples
1 · hostname
<? $hostname = "php.net"; $return = dns_get_record($hostname); print_r($return);
Array ( [0] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => TXT [txt] => v=spf1 ip4:140.211.15.143 ip4:45.112.84.5 ip4:142.93.197.176 ip6:2604:a880:400:d0::1c74:1001 ip6:2a02:cb43:8000::1102 ip4:157.90.121.187 ip6:2a01:4f8:1c1e:416d::1 ~all [entries] => Array ( [0] => v=spf1 ip4:140.211.15.143 ip4:45.112.84.5 ip4:142.93.197.176 ip6:2604:a880:400:d0::1c74:1001 ip6:2a02:cb43:8000::1102 ip4:157.90.121.187 ip6:2a01:4f8:1c1e:416d::1 ~all ) ) [1] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns4.easydns.info ) [2] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => A [ip] => 185.85.0.29 ) [3] => Array ( [host] => php.net [class] => IN [ttl] => 30 [type] => MX [pri] => 0 [target] => php-smtp4-ip4.php.net ) [4] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns2.easydns.net ) [5] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => AAAA [ipv6] => 2a02:cb40:200::1ad ) [6] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => SOA [mname] => ns1.php.net [rname] => admin.easydns.com [serial] => 1731297662 [refresh] => 16384 [retry] => 2048 [expire] => 1048576 [minimum-ttl] => 2560 ) [7] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns3.easydns.org ) [8] => Array ( [host] => php.net [class] => IN [ttl] => 300 [type] => NS [target] => dns1.easydns.com ) )
2 · type
<? $hostname = "php.net"; $type = DNS_A; $return = dns_get_record($hostname, $type); print_r($return);
Array ( [0] => Array ( [host] => php.net [class] => IN [ttl] => 299 [type] => A [ip] => 185.85.0.29 ) )
Links
Network
- checkdnsrr
- closelog
- dns_check_record
- dns_get_mx
- fsockopen
- gethostbyaddr
- gethostbyname
- gethostbynamel
- gethostname
- getmxrr
- getprotobyname
- getprotobynumber
- getservbyname
- getservbyport
- header
- header_register_callback
- header_remove
- headers_list
- headers_sent
- http_response_code
- inet_ntop
- inet_pton
- ip2long
- long2ip
- openlog
- pfsockopen
- setcookie
- setrawcookie
- socket_get_status
- socket_set_blocking
- socket_set_timeout
- syslog