{"id":19665921,"url":"https://github.com/amphp/dns","last_synced_at":"2025-05-14T21:02:48.099Z","repository":{"id":44623935,"uuid":"20811834","full_name":"amphp/dns","owner":"amphp","description":"Async DNS resolution for PHP based on Amp.","archived":false,"fork":false,"pushed_at":"2025-01-19T15:50:17.000Z","size":565,"stargazers_count":172,"open_issues_count":4,"forks_count":31,"subscribers_count":11,"default_branch":"2.x","last_synced_at":"2025-04-13T17:46:45.408Z","etag":null,"topics":["amphp","async","dns","php"],"latest_commit_sha":null,"homepage":"https://amphp.org/dns","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"amphp"}},"created_at":"2014-06-13T17:19:02.000Z","updated_at":"2025-04-03T08:15:26.000Z","dependencies_parsed_at":"2024-01-30T23:51:28.542Z","dependency_job_id":"b67d1325-e45b-4eed-a080-8954d4be4948","html_url":"https://github.com/amphp/dns","commit_stats":{"total_commits":359,"total_committers":23,"mean_commits":"15.608695652173912","dds":"0.48467966573816157","last_synced_commit":"e42876aa8306c754abd1b3e71a44e13066909fd1"},"previous_names":[],"tags_count":73,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fdns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fdns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fdns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fdns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amphp","download_url":"https://codeload.github.com/amphp/dns/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254227605,"owners_count":22035668,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amphp","async","dns","php"],"created_at":"2024-11-11T16:25:28.230Z","updated_at":"2025-05-14T21:02:48.067Z","avatar_url":"https://github.com/amphp.png","language":"PHP","readme":"# amphp/dns\n\nAMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.\n`amphp/dns` provides hostname to IP address resolution and querying specific DNS records.\n\n[![Latest Release](https://img.shields.io/github/release/amphp/dns.svg?style=flat-square)](https://github.com/amphp/dns/releases)\n[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/amphp/dns/blob/master/LICENSE)\n\n## Installation\n\nThis package can be installed as a [Composer](https://getcomposer.org/) dependency.\n\n```bash\ncomposer require amphp/dns\n```\n\n## Usage\n\n### Configuration\n\n`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.\n\nIt respects the system's hosts file on Unix and Windows based systems, so it works just fine in environments like Docker with named containers.\n\nThe 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.\n\nUsually 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.\n\n### Hostname to IP Resolution\n\n`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.\n\n```php\n// Example without type restriction. Will return IPv4 and / or IPv6 addresses.\n// What's returned depends on what's available for the given hostname.\n\n/** @var Amp\\Dns\\DnsRecord[] $records */\n$records = Amp\\Dns\\resolve(\"github.com\");\n```\n\n```php\n// Example with type restriction. Will throw an exception if there are no A records.\n\n/** @var Amp\\Dns\\DnsRecord[] $records */\n$records = Amp\\Dns\\resolve(\"github.com\", Amp\\Dns\\DnsRecord::A);\n```\n\n### Custom Queries\n\n`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.\n\n```php\n/** @var Amp\\Dns\\DnsRecord[] $records */\n$records = Amp\\Dns\\query(\"google.com\", Amp\\Dns\\DnsRecord::MX);\n```\n\n```php\n/** @var Amp\\Dns\\DnsRecord[] $records */\n$records = Amp\\Dns\\query(\"8.8.8.8\", Amp\\Dns\\DnsRecord::PTR);\n```\n\n### Caching\n\nThe `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`.\n\n### Reloading Configuration\n\nThe `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.\n\n```php\nEventLoop::repeat(600, function () use ($resolver) {\n    Amp\\Dns\\dnsResolver()-\u003ereloadConfig();\n});\n```\n\n\u003e **Note**\n\u003e The above code relies on the resolver not being changed. `reloadConfig` is specific to `Rfc1035StubResolver` and is not part of the `Resolver` interface.\n\n## Example\n\n```php\n\u003c?php\n\nrequire __DIR__ . '/examples/_bootstrap.php';\n\n$githubIpv4 = Amp\\Dns\\resolve(\"github.com\", Dns\\Record::A);\npretty_print_records(\"github.com\", $githubIpv4);\n\n$firstGoogleResult = Amp\\Future\\awaitFirst([\n  Amp\\async(fn() =\u003e Amp\\Dns\\resolve(\"google.com\", Dns\\Record::A)),\n  Amp\\async(fn() =\u003e Amp\\Dns\\resolve(\"google.com\", Dns\\Record::AAAA)),\n]);\n\npretty_print_records(\"google.com\", $firstGoogleResult);\n\n$combinedGoogleResult = Amp\\Dns\\resolve(\"google.com\");\npretty_print_records(\"google.com\", $combinedGoogleResult);\n\n$googleMx = Amp\\Dns\\query(\"google.com\", Amp\\Dns\\DnsRecord::MX);\npretty_print_records(\"google.com\", $googleMx);\n```\n","funding_links":["https://github.com/sponsors/amphp"],"categories":["DNS"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fdns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famphp%2Fdns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fdns/lists"}