PHPackages                             daverandom/addr - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. daverandom/addr

Abandoned → [amphp/dns](/?search=amphp%2Fdns)Library[Queues &amp; Workers](/categories/queues)

daverandom/addr
===============

Async DNS resolution for Amp.

v1.2.3(5y ago)1921.1k30[6 issues](https://github.com/DaveRandom/Addr/issues)MITPHPPHP &gt;=7.0CI failing

Since Jun 15Pushed 1mo ago10 watchersCompare

[ Source](https://github.com/DaveRandom/Addr)[ Packagist](https://packagist.org/packages/daverandom/addr)[ Docs](https://github.com/amphp/dns)[ GitHub Sponsors](https://github.com/amphp)[ RSS](/packages/daverandom-addr/feed)WikiDiscussions 2.x Synced 1w ago

READMEChangelog (10)Dependencies (9)Versions (67)Used By (0)

amphp/dns
=========

[](#amphpdns)

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.

[![Latest Release](https://camo.githubusercontent.com/876a051404a2e7022119f48032d5ef57dbda7d949f040adf94b809ddc2992d67/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f616d7068702f646e732e7376673f7374796c653d666c61742d737175617265)](https://github.com/amphp/dns/releases)[![MIT License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/amphp/dns/blob/master/LICENSE)

Installation
------------

[](#installation)

This package can be installed as a [Composer](https://getcomposer.org/) dependency.

```
composer require amphp/dns
```

Usage
-----

[](#usage)

### Configuration

[](#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

[](#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

[](#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

[](#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

[](#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 to `Rfc1035StubResolver` and is not part of the `Resolver` interface.

Example
-------

[](#example)

```
