amphp / dns
Async DNS resolution for Amp.
Fund package maintenance!
amphp
Installs: 13 031 056
Dependents: 20
Suggesters: 0
Security: 0
Stars: 165
Watchers: 12
Forks: 32
Open Issues: 5
Requires
- php: >=8.1
- ext-filter: *
- ext-json: *
- amphp/amp: ^3
- amphp/byte-stream: ^2
- amphp/cache: ^2
- amphp/parser: ^1
- amphp/process: ^2
- daverandom/libdns: ^2.0.2
- revolt/event-loop: ^1 || ^0.2
Requires (Dev)
- amphp/php-cs-fixer-config: ^2
- amphp/phpunit-util: ^3
- phpunit/phpunit: ^9
- psalm/phar: 5.20
- 2.x-dev
- v2.3.0
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v2.0.0-beta.5
- v2.0.0-beta.4
- v2.0.0-beta.3
- v2.0.0-beta.2
- v2.0.0-beta.1
- 1.x-dev
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- v0.9.x-dev
- v0.9.15
- v0.9.14
- v0.9.13
- v0.9.12
- v0.9.11
- v0.9.10
- v0.9.9
- v0.9.8
- v0.9.7
- v0.9.6
- v0.9.5
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.15
- v0.8.14
- v0.8.13
- v0.8.12
- v0.8.11
- v0.8.10
- v0.8.9
- v0.8.8
- v0.8.7
- v0.8.6
- v0.8.5
- v0.8.4
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.1
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-issue-120
- dev-dep/libdns-v3
This package is auto-updated.
Last update: 2024-12-21 01:21:17 UTC
README
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.
amphp/dns
provides hostname to IP address resolution and querying specific DNS records.
Installation
This package can be installed as a Composer dependency.
composer require amphp/dns
Usage
Configuration
amphp/dns
automatically detects the system configuration and uses it. On Unix-like systems it reads /etc/resolv.conf
and respects settings for nameservers, timeouts, and attempts. On Windows it looks up the correct entries in the Windows Registry and takes the listed nameservers. You can pass a custom ConfigLoader
instance to Rfc1035StubResolver
to load another configuration, such as a static config.
It respects the system's hosts file on Unix and Windows based systems, so it works just fine in environments like Docker with named containers.
The package uses a global default resolver which can be accessed and changed via Amp\Dns\resolver()
. If an argument other than null
is given, the resolver is used as global instance.
Usually you don't have to change the resolver. If you want to use a custom configuration for a certain request, you can create a new resolver instance and use that instead of changing the global one.
Hostname to IP Resolution
Amp\Dns\resolve
provides hostname to IP address resolution. It returns an array of IPv4 and IPv6 addresses by default. The type of IP addresses returned can be restricted by passing a second argument with the respective type.
// Example without type restriction. Will return IPv4 and / or IPv6 addresses. // What's returned depends on what's available for the given hostname. /** @var Amp\Dns\DnsRecord[] $records */ $records = Amp\Dns\resolve("github.com");
// Example with type restriction. Will throw an exception if there are no A records. /** @var Amp\Dns\DnsRecord[] $records */ $records = Amp\Dns\resolve("github.com", Amp\Dns\DnsRecord::A);
Custom Queries
Amp\Dns\query
supports the various other DNS record types such as MX
, PTR
, or TXT
. It automatically rewrites passed IP addresses for PTR
lookups.
/** @var Amp\Dns\DnsRecord[] $records */ $records = Amp\Dns\query("google.com", Amp\Dns\DnsRecord::MX);
/** @var Amp\Dns\DnsRecord[] $records */ $records = Amp\Dns\query("8.8.8.8", Amp\Dns\DnsRecord::PTR);
Caching
The Rfc1035StubResolver
caches responses by default in an Amp\Cache\LocalCache
. You can set any other Amp\Cache\Cache
implementation by creating a custom instance of Rfc1035StubResolver
and setting that via Amp\Dns\resolver()
, but it's usually unnecessary. If you have a lot of very short running scripts, you might want to consider using a local DNS resolver with a cache instead of setting a custom cache implementation, such as dnsmasq
.
Reloading Configuration
The Rfc1035StubResolver
(which is the default resolver shipping with that package) will cache the configuration of /etc/resolv.conf
/ the Windows Registry and the read host files by default. If you wish to reload them, you can set a periodic timer that requests a background reload of the configuration.
EventLoop::repeat(600, function () use ($resolver) { Amp\Dns\dnsResolver()->reloadConfig(); });
Note The above code relies on the resolver not being changed.
reloadConfig
is specific toRfc1035StubResolver
and is not part of theResolver
interface.
Example
<?php require __DIR__ . '/examples/_bootstrap.php'; $githubIpv4 = Amp\Dns\resolve("github.com", Dns\Record::A); pretty_print_records("github.com", $githubIpv4); $firstGoogleResult = Amp\Future\awaitFirst([ Amp\async(fn() => Amp\Dns\resolve("google.com", Dns\Record::A)), Amp\async(fn() => Amp\Dns\resolve("google.com", Dns\Record::AAAA)), ]); pretty_print_records("google.com", $firstGoogleResult); $combinedGoogleResult = Amp\Dns\resolve("google.com"); pretty_print_records("google.com", $combinedGoogleResult); $googleMx = Amp\Dns\query("google.com", Amp\Dns\DnsRecord::MX); pretty_print_records("google.com", $googleMx);