dns_get_record()
dns_get_record() — Fetch DNS Resource Records associated with a hostname.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
dns_get_record(string $hostname, int $type = DNS_ANY, array &$authoritative_name_servers = null, array &$additional_records = null, bool $raw = false): array|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $hostname | string | 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. |
| $type | int | By default, dns_get_record() will search for any resource records associated with hostname. To limit the query, use one of the DNS_* constants. |
| &$authoritative_name_servers | array | Passed by reference and, if given, will be populated with Resource Records for the Authoritative Name Servers. |
| &$additional_records | array | Passed by reference and, if given, will be populated with any Additional Records. |
| $raw | bool | 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 value
This function returns an array of associative arrays, or false on failure. 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 dependent 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 ...
Example
A runnable snippet:
$result = dns_get_record("php.net"); print_r($result);
Output:
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 ) )
Changelog
| Version | Change |
|---|---|
| 7.0.16, 7.1.2 | Added support for CAA record type. |
See also
dns_get_mx(), dns_check_record()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page dns_get_record() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.