spatie/laravel-rdap
Laravel package for performing RDAP lookups (WHOIS successor) to fetch domain registration data as structured JSON. Includes built-in caching for TLD server discovery and RDAP responses, plus configurable retries/timeouts for unreliable endpoints.
Installation:
composer require spatie/laravel-rdap
php artisan vendor:publish --tag="rdap-config"
This publishes the config file (config/rdap.php) with default caching and retry settings.
First Query: Use the facade to fetch domain or IP data:
use Spatie\Rdap\Facades\Rdap;
// Domain query
$domain = Rdap::domain('google.com');
// IP query
$ip = Rdap::ip('127.0.0.1');
Check TLD Support: Verify if a domain's TLD is supported by RDAP:
if (Rdap::domainIsSupported('example.com')) {
// Proceed with query
}
Basic Usage:
$domain = Rdap::domain('example.com');
$expiryDate = $domain->expirationDate(); // Carbon instance
$status = $domain->hasStatus(DomainStatus::ClientTransferProhibited);
Custom Servers: Override the default RDAP server for specific queries:
$domain = Rdap::domain('example.com', dnsServer: 'https://custom-rdap.example.com');
Retry Configuration: Adjust retry logic for unreliable RDAP endpoints:
$domain = Rdap::domain('example.com', [
'timeoutInSeconds' => 10,
'retryTimes' => 5,
'sleepInMillisecondsBetweenRetries' => 2000,
]);
Data Extraction: Access nested properties using dot notation:
$registrar = $domain->get('events.0.eventAction'); // e.g., 'registration'
$allData = $domain->all(); // Full response as array
Basic Usage:
$ip = Rdap::ip('8.8.8.8');
$network = $ip->get('objectClassName'); // 'ip network'
Date Handling:
$ip->registrationDate(); // Carbon instance
$ip->lastUpdateOfRdapDb();
Fetch Server for TLD:
$serverUrl = Rdap::dns()->getServerForTld('com');
List Supported TLDs:
$supportedTlds = Rdap::dns()->supportedTlds();
IP Registry Lookup:
$ipServer = (new \Spatie\Rdap\RdapIpV4())->getServerForIp('8.8.8.8');
Caching:
Leverage built-in caching for TLD servers (default: 1 week) and domain/IP responses (if extended via allnetru's PR).
// Override cache store (e.g., Redis)
'tld_servers_cache' => [
'store_name' => 'redis',
'duration_in_seconds' => CarbonInterval::day()->totalSeconds,
],
Error Handling:
Catch RdapException for timeouts or invalid responses:
try {
$domain = Rdap::domain('unreachable.example');
} catch (\Spatie\Rdap\Exceptions\RdapException $e) {
// Log or retry logic
}
Testing: Mock the facade or use stubs for unit tests:
Rdap::shouldReceive('domain')->andReturn(new DomainResponse());
Unsupported TLDs:
RDAP does not support all TLDs (e.g., .be). Always check domainIsSupported() first.
if (!Rdap::domainIsSupported('example.be')) {
throw new \RuntimeException('RDAP not available for .be');
}
Rate Limiting: RDAP servers may throttle requests. Implement exponential backoff for retries:
$sleepTime = 1000 * (2 ** $retryAttempt);
Invalid Responses:
Some RDAP servers return malformed JSON. Use the InvalidRdapResponse exception to handle this gracefully.
IPv6 Support:
The package primarily supports IPv4. For IPv6, use RdapIpV6 (if extended) or validate input:
if (filter_var('2001:db8::1', FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
// Handle IPv6
}
Log Raw Responses: Inspect raw RDAP responses for debugging:
$domain = Rdap::domain('example.com');
\Log::debug('Raw RDAP response:', $domain->all());
Custom DNS Server: Test with a local RDAP server (e.g., Mock RDAP) to debug issues:
Rdap::domain('test.local', dnsServer: 'http://localhost:3000');
Cache Invalidation: Clear caches when TLD server lists update (e.g., via IANA):
php artisan cache:clear
Add Domain/IP Caching: Extend the package to cache responses (see PR #47):
// Example: Cache domain responses for 1 hour
$domain = Rdap::domain('example.com');
Cache::put("rdap_domain_{$domain->get('domainName')}", $domain, now()->addHour());
Custom Status Enums:
Extend DomainStatus enum for project-specific checks:
use Spatie\Rdap\Enums\DomainStatus;
class CustomDomainStatus extends DomainStatus {
public const PendingDeletion = 'pending deletion';
}
Batch Queries: Implement batch processing for multiple domains/IPs:
$domains = collect(['example.com', 'test.org'])
->map(fn ($domain) => Rdap::domain($domain))
->filter();
Webhook Integration: Trigger actions on domain/IP changes (e.g., expiry alerts):
$domain = Rdap::domain('example.com');
if ($domain->expirationDate()->lt(now()->addDays(7))) {
// Send expiry alert
}
How can I help you explore Laravel packages today?