Home PHP Network dns_get_record() PHP dns_get_record() Function Definition In PHP, dns_get_record() fetches DNS resource records for a hostname and returns them as an array of associative arrays. On failure it returns false.
You can limit the query with DNS_* constants such as DNS_A or DNS_MX. Optional reference arguments can receive authoritative and additional record sets. This is the detailed counterpart to checkdnsrr().
Syntax Call dns_get_record() with the values you need. Start with this line.
$records = dns_get_record("example.com", DNS_A); Call What it does dns_get_record("example.com")Fetches any available records (DNS_ANY) as an array. dns_get_record("example.com", DNS_MX)Fetches only MX records with pri and target fields. dns_get_record("example.com", DNS_A, $ns, $add)Also fills authoritative and additional record arrays.
Each result row usually includes host, class, type and ttl, plus type-specific keys such as ip for A or target for MX. Pass DNS_* constants for $type unless $raw is true.
Library declarations 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 Description hostname Required. The DNS host name to look up, for example "www.example.com". type Optional. A DNS_* constant selecting which records to fetch. Default is DNS_ANY. authoritative_name_servers Optional. Passed by reference; receives authoritative name server records when provided. additional_records Optional. Passed by reference; receives additional records when provided. raw Optional. When true, $type is treated as a raw DNS type ID and results include a data key to parse yourself.
Return Value Return Value Description array A list of associative arrays describing each matching DNS record. false Returned when the lookup fails.
PHP Examples 1. Fetch A Records 2. Read MX Priority and Target 3. Inspect Keys on One Record Example 1 — Fetch A Records Request A records and print the IP from each row. Output below is a typical sample, not a live query.
<?php
$records = dns_get_record("example.com", DNS_A);
if ($records === false) {
echo "Lookup failed", PHP_EOL;
} else {
foreach ($records as $row) {
echo $row["type"], " ", $row["ip"], PHP_EOL;
}
} How It Works DNS_A limits the query to IPv4 address records. false means the lookup failed. Each row is an associative array with keys like type and ip. The printed address is a well-known sample for example. com.Example 2 — Read MX Priority and Target Fetch MX records and show pri plus target from the returned arrays.
<?php
$records = dns_get_record("example.com", DNS_MX);
foreach ($records as $row) {
echo $row["pri"], " ", $row["target"], PHP_EOL;
} 10 mail.example.com
20 backup-mail.example.com How It Works DNS_MX requests mail exchanger records only. MX rows include pri (priority) and target (host). Lower pri values are usually preferred. The two lines are fixed demo data for teaching. Frequently Asked Questions What does dns_get_record() return? It returns an array of associative arrays describing DNS records, or false if the lookup fails.
How do I fetch only MX records? Pass DNS_MX as the second argument: dns_get_record($host, DNS_MX). Each row then includes pri and target.
What is the difference between dns_get_record() and checkdnsrr()? checkdnsrr() only returns true or false. dns_get_record() returns the actual record data.
What do the reference parameters do? If you pass extra array variables, PHP can fill them with authoritative name server records and additional records related to the answer.
Why did I get false instead of an array? The DNS query failed. Check the hostname spelling, network access and that DNS functions are available on your platform.
Next: fsockopen() fsockopen() opens a socket connection to a host and port (or to a Unix-domain transport) a.
fsockopen() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion