NetDNS2 A Pure PHP DNS Resolver and Updater
Query, update, and verify DNS from PHP without shelling out to dig or linking against libresolv. NetDNS2 covers almost every feature modern DNS servers support, and runs anywhere PHP does. It began as the PEAR package Net_DNS2, and has been maintained since 2010.
composer require mikepultz/netdns2
PHP DNS Resolver Features
Encrypted and Plain Transports
UDP, TCP, DNS over TLS, and DNS over HTTPS (RFC 8484 application/dns-message), to IPv4 or IPv6 name servers.
Every Record Type
All defined, non-obsolete resource record types, returned as typed PHP objects. See the full list.
DNSSEC
DNSSEC requests and records, with client-side signature verification.
Updates and Zone Transfers
Dynamic updates through the Updater class, NOTIFY messages through the Notifier, and AXFR, all signable with TSIG or SIG(0).
EDNS(0) Options
Client subnet, cookies, TCP keepalive, NSID, extended DNS errors, and more.
Local Cache
Cache answers in shared memory, a flat file, Memcached, or Redis to cut repeat lookups.
Internationalized domain names are handled too. The README on GitHub has every config option and example.
Installation
Install with Composer:
composer require mikepultz/netdns2
Then look up a record:
try
{
$r = new \NetDNS2\Resolver([ 'nameservers' => [ '1.1.1.1' ] ]);
$res = $r->query('example.com', 'A');
echo $res->answer[0]->address;
} catch(\NetDNS2\Exception $e)
{
echo "query failed: " . $e->getMessage() . "\n";
}
Requirements
PHP 8.1 or newer. These extensions are optional, and each turns on a feature:
openssl | DNS over TLS, some record types |
curl | DNS over HTTPS |
hash | TSIG signing |
intl | Unicode domain names |
sodium | ED25519 DNSSEC verification |
shmop, memcached, redis | Local caching |
Frequently Asked Questions
How Do I Look Up DNS Records in PHP with a Specific Name Server?
Create a \NetDNS2\Resolver with a nameservers array, then call query() with the name and record type, for example $r->query('example.com', 'MX'). The records come back in $res->answer. Leave nameservers out and NetDNS2 uses the servers in /etc/resolv.conf.
How Is NetDNS2 Different from PHP's dns_get_record()?
PHP's built-in dns_get_record() has no way to choose which name server it asks, and it only handles the record types it has DNS_* constants for, such as A, MX, TXT, and SRV. NetDNS2 lets you pick the server and the transport, handles every current record type including DNSSEC, and can also send dynamic updates.
Does NetDNS2 Support DNS over HTTPS and DNS over TLS?
Yes, both since version 2.0. For DNS over TLS, set use_tls to true; queries then go over TCP to port 853, and the OpenSSL extension is required. For DNS over HTTPS, pass a URL as the name server, such as https://cloudflare-dns.com/dns-query; this needs the cURL extension and uses the RFC 8484 wire format.
How Do I Upgrade from Net_DNS2 1.x?
Version 2 needs PHP 8.1 and moved to PSR-4 namespaces, so the Net_DNS2_ class prefix becomes the \NetDNS2\ namespace: Net_DNS2_Resolver is now \NetDNS2\Resolver, and Net_DNS2_Exception is \NetDNS2\Exception. Most class, method, and property names are unchanged. The upgrade notes show before and after examples.
Is Net_DNS2 1.x Still Maintained?
Yes. The README says 1.x will be maintained for the foreseeable future, and the latest 1.x release, v1.5.5, came out in May 2025. It runs on PHP 7.1 or newer. To stay on it, require "mikepultz/netdns2": "^1.5" in your composer.json. Every release is listed in the change log.
Sponsored by Generator Labs
Generator Labs sponsors NetDNS2's development and runs it in its own platform. Generator Labs watches the things that quietly break mail and websites: Blacklist Monitoring checks your IPs and domains against hundreds of blocklists, and Certificate Monitoring catches expiring, misconfigured, or revoked certificates before your users do.
Visit Generator LabsContact
Bugs and Feature Requests
Open a GitHub issue.
Contributing
Pull requests on GitHub are welcome.
Security Issues
Report vulnerabilities privately through GitHub security advisories.