PHPackages                             spatie/dns - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/dns

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/dns
==========

Retrieve DNS records

2.7.1(5mo ago)6082.3M↑21.4%10017MITPHPPHP ^8.2CI passing

Since Nov 3Pushed 4mo ago11 watchersCompare

[ Source](https://github.com/spatie/dns)[ Packagist](https://packagist.org/packages/spatie/dns)[ Docs](https://github.com/spatie/dns)[ RSS](/packages/spatie-dns/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (41)Used By (17)

Retrieve DNS records
====================

[](#retrieve-dns-records)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ddaa55a0316dc9808d7651869a6c332b8efb8318313949bc834a280de3491cfe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f646e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/dns)[![Tests](https://github.com/spatie/dns/actions/workflows/tests.yml/badge.svg)](https://github.com/spatie/dns/actions/workflows/tests.yml)[![Code style](https://github.com/spatie/dns/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/spatie/dns/actions/workflows/php-cs-fixer.yml)[![Total Downloads](https://camo.githubusercontent.com/a533e388d4a444d2c538919b94abcfb7e48444a1abe430d3f7bbc5c6a9008793/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f646e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/dns)

This package contains a class that can fetch DNS records.

```
use Spatie\Dns\Dns;

$dns = new Dns();

$dns->getRecords('spatie.be'); // returns all available dns records

$dns->getRecords('spatie.be', 'A'); // returns only A records
```

You can use various methods to retrieve info of a record.

```
$records = $dns->getRecords('spatie.be')

$hostNameOfFirstRecord = $records[0]->host();
```

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/40c70d8a455de11aca025219313f93e0419be15b33f57cde06283aa0da5bb5ac/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f646e732e6a70673f743d31)](https://spatie.be/github-ad-click/dns)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

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

[](#installation)

If you do not have [dig](https://linux.die.net/man/1/dig) installed you will need it.

You can install the package via composer:

```
composer require spatie/dns
```

Usage
-----

[](#usage)

The class can get these record types: `A`, `AAAA`, `CNAME`, `NS`, `PTR`, `SOA`, `MX`, `SRV`, `TXT`, `DNSKEY`, `CAA`, `NAPTR`.

```
use Spatie\Dns\Dns;

$dns = new Dns();

$dns->getRecords('spatie.be'); // returns all available dns records

$dns->getRecords('spatie.be', 'A'); // returns only A records
$dns->getRecords('spatie.be', ['A', 'CNAME']); // returns both A and CNAME records
$dns->getRecords('spatie.be', DNS_MX); // returns only MX records
$dns->getRecords('spatie.be', DNS_A | DNS_AAAA); // returns both A and AAAA records
```

`getRecords` will return an array with objects that implement the `Spatie\Dns\Records\Record` interface.

Working with DNS records
------------------------

[](#working-with-dns-records)

Here's how you can fetch the first A-record of a domain.

```
$ARecord = $dns->getRecords('spatie.be', 'A')[0];
```

These methods can be called on all records:

- `host()`: returns the host (`spatie.be`)
- `ttl()`: return the time to live (`900`)
- `class()`: returns the class (`IN`)
- `type()`: returns the type (`A`)

When converting a record to a string you'll get a string with all info separated with tabs.

```
(string)$ARecord // returns `spatie.be.              900     IN      A       138.197.187.74`
```

Some records have additional methods available. For example, records of type A [have an additional `ip()` method](https://github.com/spatie/dns/blob/72bf709a44e19e5d8f0bc7e6c93cf70e7a1b18f3/src/Records/A.php#L6). To know which extra methods there are, check the docblocks above [all record classes](https://github.com/spatie/dns/tree/72bf709a44e19e5d8f0bc7e6c93cf70e7a1b18f3/src/Records) in the source code.

Using a specific nameserver
---------------------------

[](#using-a-specific-nameserver)

You can get records from a specific nameserver.

```
use Spatie\Dns\Dns;

(new Dns)
    ->useNameserver('ns1.openminds.be') // use ns1.openminds.be
    ->getRecords('spatie.be');
```

Specify retries and timeouts for Dig
------------------------------------

[](#specify-retries-and-timeouts-for-dig)

Dig can be configured to retry a DNS query after a certain timeout. Use `setRetries()` or `setTimeout()` to configure those settings. The timeout is specified in seconds.

```
use Spatie\Dns\Dns;

(new Dns)
    ->setRetries(4) // try 4 times
    ->setTimeout(5) // wait 5 seconds for a reply from the nameserver
    ->getRecords('spatie.be');
```

Guessing a record
-----------------

[](#guessing-a-record)

When you have a string that contains a dns record, you can convert it to a `Record`

```
use \Spatie\Dns\Support\Factory();

// returns instance of \Spatie\Dns\Records\CNAME
(new Factory())->guess('www.spatie.be.       300     IN      CNAME   spatie.be.');
```

Using custom handlers
---------------------

[](#using-custom-handlers)

A `Handler` is responsible for fetching DNS records of a certain type.

By default, these handlers that ship with the package are used:

- `Spatie\Dns\Handlers\Dig`
- `Spatie\Dns\Handlers\DnsGetRecord`

You can create custom handlers. A valid handler is any class that extends from `Spatie\Dns\Handlers\Handler`.

A custom handler class can be used by passing it to `useHandlers` on `Spatie\Dns\Dns`.

```
$results = (new Dns)
    ->useHandlers([new YourCustomHandler()])
    ->getRecords('spatie.be');
```

Under the hood
--------------

[](#under-the-hood)

We will use [dig](https://wiki.ubuntuusers.de/dig/) to fetch DNS info. If it is not installed on your system, we'll call the native `dns_get_record()` function.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

### Security

[](#security)

If you've found a bug regarding security please mail .

Postcardware
------------

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Harish Toshniwal](https://github.com/introwit)
- [Tom Witkowski](https://github.com/Gummibeer)
- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

70

—

ExcellentBetter than 100% of packages

Maintenance73

Regular maintenance activity

Popularity65

Solid adoption and visibility

Community41

Growing community involvement

Maturity88

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 64.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~77 days

Recently: every ~177 days

Total

39

Last Release

173d ago

Major Versions

0.0.1 → 1.0.02017-11-03

1.6.0 → 2.0.02021-05-20

PHP version history (6 changes)0.0.1PHP ^7.0

1.3.1PHP ^7.1

1.4.3PHP ^7.4

1.6.0PHP ^7.4|^8.0

2.0.0PHP ^8.0

2.7.1PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (180 commits)")[![Gummibeer](https://avatars.githubusercontent.com/u/6187884?v=4)](https://github.com/Gummibeer "Gummibeer (32 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (9 commits)")[![owenvoke](https://avatars.githubusercontent.com/u/1899334?v=4)](https://github.com/owenvoke "owenvoke (7 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (6 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (4 commits)")[![ManojKiranA](https://avatars.githubusercontent.com/u/30294553?v=4)](https://github.com/ManojKiranA "ManojKiranA (3 commits)")[![thinkstylestudio](https://avatars.githubusercontent.com/u/322368?v=4)](https://github.com/thinkstylestudio "thinkstylestudio (3 commits)")[![introwit](https://avatars.githubusercontent.com/u/11228182?v=4)](https://github.com/introwit "introwit (3 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (3 commits)")[![Kovah](https://avatars.githubusercontent.com/u/1816101?v=4)](https://github.com/Kovah "Kovah (3 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (2 commits)")[![lwohn](https://avatars.githubusercontent.com/u/107246509?v=4)](https://github.com/lwohn "lwohn (2 commits)")[![jasonlfunk](https://avatars.githubusercontent.com/u/685188?v=4)](https://github.com/jasonlfunk "jasonlfunk (2 commits)")[![rennokki](https://avatars.githubusercontent.com/u/21983456?v=4)](https://github.com/rennokki "rennokki (2 commits)")[![SRWieZ](https://avatars.githubusercontent.com/u/1408020?v=4)](https://github.com/SRWieZ "SRWieZ (2 commits)")[![thecaliskan](https://avatars.githubusercontent.com/u/13554944?v=4)](https://github.com/thecaliskan "thecaliskan (2 commits)")[![Fuitad](https://avatars.githubusercontent.com/u/263288?v=4)](https://github.com/Fuitad "Fuitad (2 commits)")[![hostep](https://avatars.githubusercontent.com/u/85479?v=4)](https://github.com/hostep "hostep (2 commits)")[![wmouwen](https://avatars.githubusercontent.com/u/13197752?v=4)](https://github.com/wmouwen "wmouwen (1 commits)")

---

Tags

dnshacktoberfestphpspatiedns

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/spatie-dns/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-dns/health.svg)](https://phpackages.com/packages/spatie-dns)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/url

Parse, build and manipulate URL's

73914.3M97](/packages/spatie-url)[spatie/typescript-transformer

This is my package typescript-transformer

3706.5M16](/packages/spatie-typescript-transformer)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
