spatie/dns
Fetch DNS records in PHP using dig. Query domains for A, AAAA, CNAME, MX, TXT, SRV and more, filter by type(s), and get structured record objects with handy accessors for record details.
spatie/dns package is a lightweight, focused solution for DNS record retrieval (A, MX, TXT, etc.), making it ideal for applications requiring DNS validation, email routing, or domain verification (e.g., OAuth, SPF/DKIM checks, or custom DNS lookups).dns_get_record(), but extendable via DnsServiceProvider). Enables A/B testing or fallback logic (e.g., primary Cloudflare, secondary system DNS).DnsRecordResolver) simplify unit/integration testing for DNS-dependent logic.dns:clear-cache Artisan command).dns_get_record() may not handle IPv6 uniformly across PHP versions. Test thoroughly if IPv6 is critical.DnsRecordResolver interface supports this.config/app.php or a custom service provider. Inject Dns facade or resolver into controllers/services.php artisan vendor:publish --provider="Spatie\Dns\DnsServiceProvider") to customize providers or defaults.dns:check for validation).Dns facade for simple lookups:
use Spatie\Dns\Dns;
$records = Dns::get('example.com', 'MX');
// config/dns.php
'providers' => [
'cloudflare' => [
'enabled' => true,
'api_token' => env('CLOUDFLARE_API_TOKEN'),
],
],
$this->app->bind(DnsRecordResolver::class, function () {
return new CustomDnsResolver();
});
$cachedResolver = Cache::remember("dns_{$domain}_{$type}", now()->addMinutes(5), function () use ($domain, $type) {
return Dns::get($domain, $type);
});
spatie/cloudflare-dns).dns_get_record() or custom providers.dns:clear-cache Artisan command to invalidate stale records.Dns::get() with debug:dns (if extended) to log raw responses.spatie/async-dns if available).dns_get_record() performance under load (system DNS may throttle).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| DNS Provider Unavailable | App features break (e.g., email validation). | Fallback to system DNS or queue retries. |
| Cache Stale | Outdated records (e.g., expired SPF). | Short TTLs + cache invalidation. |
| Rate Limiting (Cloud Providers) | API throttling. | Implement exponential backoff or local caching. |
PHP dns_get_record() Bugs |
IPv6/edge record failures. | Test on target PHP versions; use custom provider. |
| Credential Leaks | API keys exposed in logs. | Use Laravel’s env() or vaults (e.g., HashiCorp). |
How can I help you explore Laravel packages today?