1. php
  2. /references
  3. /dns-get-record

PHP dns_get_record() Function

PHP's dns_get_record() retrieves DNS resource records for a given domain name. For example:

<?php

$records = dns_get_record('webreference.com', DNS_A);

foreach ($records as $record) {
    echo $record['ip'] . "\n";
}

?>

Syntax and Parameters

array dns_get_record ( string $hostname [, int $type = DNS_ANY [, array &$authns [, array &$addtl [, bool $raw = false ]]]] )
ParameterDescriptionOptional/Required
$hostnameThe domain name to look up.Required
$typeThe type of record to look up.Optional
$authnsAn array of authority nameserver records.Optional
$addtlAn array of additional records.Optional
$rawIf true, return the raw data.Optional

The function will return an array of DNS resource records matching the specified domain name and record type. Each element in the array represents a single resource record, and the array is sorted in the order the records are returned by the DNS server.

The $type parameter can be any of the following values:

ConstantDescription
DNS_AIPv4 addresses
DNS_AAAAIPv6 addresses
DNS_CNAMECanonical name records
DNS_MXMail exchange records
DNS_NSName server records
DNS_PTRPointer records
DNS_SOAStart of authority records
DNS_TXTText records
DNS_HINFOHost information records
DNS_SRVService records
DNS_NAPTRNaming authority pointer records
DNS_A6IPv6 address records (deprecated)
DNS_ANY (Default)Look up all types of records
DNS_ALLLook up all types of records

There is no functional difference between DNS_ANY and DNS_ALL except for some eccentricities in how they return results. DNS_ALL is slower, but more reliable.

$authns can be useful in a variety of contexts, such as:

  • Debugging DNS issues: If you are experiencing problems with a domain's DNS records, you may want to check the authority nameserver records to ensure that they are correct.

  • Analyzing DNS configurations: By looking at a domain's authority nameserver records, you can get a sense of how the domain's DNS is set up and whether it is using any third-party DNS services.

The $addtl parameter can be used to fetch DNS resource records that are not typically used in the resolution of domain names, but that may contain useful information. You can use it to check for the presence of DNSSEC records or to gather information about a domain's SSH or TLS configuration.

  • DS records: Delegation signer records, which are used to secure a domain's DNS records with DNSSEC.

  • SSHFP records: SSH fingerprint records, which are used to associate a domain with a particular SSH host key.

  • TLSA records: Transport Layer Security Association records, which are used to associate a domain with a particular TLS certificate.

$raw when set to true will do exactly what it says and return the raw result of the DNS query.

Additional examples

Look up MX records for a domain

<?php
$records = dns_get_record('webreference.com', DNS_MX);

foreach ($records as $record) {
    echo $record['target'] . "\n";
}
?>

Look up all DNS records for a domain

<?php
$records = dns_get_record('webreference.com', DNS_ANY);

foreach ($records as $record) {
    echo $record['type'] . ' ' . $record['target'] . "\n";
}
?>

gethostbyname()

gethostbynamel()

gethostbyaddr()

checkdnsrr()