PHP dns_get_record() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

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.

PHP
$records = dns_get_record("example.com", DNS_A);
CallWhat 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
PHP
dns_get_record(string $hostname, int $type = DNS_ANY, ?array &$authoritative_name_servers = null, ?array &$additional_records = null, bool $raw = false): array|false

Parameters

ParameterDescription
hostnameRequired. The DNS host name to look up, for example "www.example.com".
typeOptional. A DNS_* constant selecting which records to fetch. Default is DNS_ANY.
authoritative_name_serversOptional. Passed by reference; receives authoritative name server records when provided.
additional_recordsOptional. Passed by reference; receives additional records when provided.
rawOptional. When true, $type is treated as a raw DNS type ID and results include a data key to parse yourself.

Return Value

Return ValueDescription
arrayA list of associative arrays describing each matching DNS record.
falseReturned when the lookup fails.

PHP Examples

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
<?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

  1. DNS_A limits the query to IPv4 address records.
  2. false means the lookup failed.
  3. Each row is an associative array with keys like type and ip.
  4. 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
<?php
$records = dns_get_record("example.com", DNS_MX);

foreach ($records as $row) {
    echo $row["pri"], " ", $row["target"], PHP_EOL;
}

How It Works

  1. DNS_MX requests mail exchanger records only.
  2. MX rows include pri (priority) and target (host).
  3. Lower pri values are usually preferred.
  4. The two lines are fixed demo data for teaching.

Example 3 — Inspect Keys on One Record

Take the first A record and list its keys so beginners can see the structure.

PHP
<?php
$records = dns_get_record("example.com", DNS_A);
$row = $records[0];

foreach (array_keys($row) as $key) {
    echo $key, PHP_EOL;
}

How It Works

  1. We fetch A records into $records.
  2. The first row is stored in $row.
  3. array_keys() lists the field names on that row.
  4. A records typically expose host, class, ttl, type and ip.

Frequently Asked Questions

It returns an array of associative arrays describing DNS records, or false if the lookup fails.
Pass DNS_MX as the second argument: dns_get_record($host, DNS_MX). Each row then includes pri and target.
checkdnsrr() only returns true or false. dns_get_record() returns the actual record data.
If you pass extra array variables, PHP can fill them with authoritative name server records and additional records related to the answer.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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