How to check the expiry date of a domain with PHP

If you are managing a large portfolio of domains and are familiar with programming, it may be useful to create some tools that keep track of important details like the expiry date of domains.

To check the expiry date of a domain using PHP, you can use the whois information for the domain. PHP does not have a built-in function to fetch whois data directly, but you can either use a command-line whois tool accessible on your server or a PHP library that can perform whois lookups.

Here are two approaches to achieve this:

1. Using a PHP Library for whois

You can use a third-party library to simplify the process. One popular library is phpWhois. Here’s how you can use it:

1. Install the phpWhois Library If you’re using Composer, you can install the library by running:

composer require phpwhois/phpwhois

2. Sample PHP Code to Check Domain Expiry

require_once 'vendor/autoload.php';

$whois = new Whois\WhoisClient();
try {
    $result = $whois->lookup('example.com');
    if (isset($result['regrinfo']['domain']['expires'])) {
        $expiryDate = $result['regrinfo']['domain']['expires'];
        echo "The domain expires on: " . $expiryDate;
    } else {
        echo "Expiry date not found.";
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

This script checks the expiry date of the domain example.com. Adjust the domain name as needed.

2. Using Command-Line whois Tool

If you have access to shell commands from PHP, you can use the whois command available on many Unix-like systems:

1. Sample PHP Code Using Shell Execution

$domain = 'example.com';
$output = shell_exec("whois $domain");
if (preg_match('/Expiry Date:\s*(.+)\n/', $output, $matches)) {
    echo "The domain expires on: " . $matches[1];
} else {
    echo "Expiry date not found.";
}

Ensure that the whois command is installed on your server and that shell_exec is enabled in your PHP configuration. Also, the output pattern in preg_match (/Expiry Date:\s*(.+)\n/) might need adjustment based on the specific whois server response, as it can vary.

Important Notes

  • Permission and Security: Using shell_exec can pose security risks, especially if the input is not properly sanitized. Always ensure that any input passed to shell commands is controlled to prevent injection attacks.
  • Availability and Limits: Be aware that frequent whois queries can lead to your IP being temporarily blocked by whois servers due to rate limiting. Use it judiciously.

Choose the method that best fits your server configuration and security guidelines. If you have a high volume of requests or need more reliable uptime, consider using a dedicated API service for domain information, which may be more stable and scalable than scraping whois data.

Is there another service without query limits?

If you’re facing query limits with standard WHOIS services, you might consider using a commercial WHOIS API that provides higher or no limits on the number of queries you can make. These services often come with a cost but offer more reliability, better support, and additional features like JSON-formatted responses, which are easier to integrate and use in your applications. Here are a few popular options:

1. DomainTools

DomainTools offers extensive services around domain and DNS research, including WHOIS lookups. Their services are robust but can be on the pricier side, aimed more at enterprises and security researchers.

2. WhoisXML API

WhoisXML API provides real-time WHOIS data with a comprehensive RESTful API. They offer various packages, some of which come with unlimited WHOIS lookups, and are widely used for domain, DNS, IP, and cyber threat intelligence.

3. JsonWHOIS

JsonWHOIS provides a modern API that returns WHOIS data in JSON format, making it easy to integrate with web applications. They offer different tiers of subscription plans based on query volumes.

4. APIs.guru OpenAPI Directory

This is a collection of machine-readable API specifications, including WHOIS APIs that might offer better limits or different capabilities compared to traditional services.

5. ICANN’s Registration Data Access Protocol (RDAP)

RDAP provides a standardized way to access domain registration data, which is a successor to WHOIS. It’s designed to address many issues related to WHOIS and provides data in a structured format. RDAP services are offered by many domain registrars and may come with different usage policies than traditional WHOIS.

Using APIs

Using a commercial API generally involves the following steps:

  1. Sign up for the service and obtain an API key.
  2. Integrate the API into your application using their documentation.
  3. Handle the API response to fetch and display the data as needed.

Example: Integrating WhoisXML API

Here’s a simple example of how you might use a WHOIS API in PHP:

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => "https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey=YOUR_API_KEY&domainName=example.com&outputFormat=JSON",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "GET",
]);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    $data = json_decode($response, true);
    echo "Domain Expiry Date: " . $data['WhoisRecord']['expiresDate'];
}

Make sure to replace "YOUR_API_KEY" with your actual API key from WhoisXML API or whichever service you choose.

Is RDAP free to use?

Yes, RDAP (Registration Data Access Protocol) is generally free to use. RDAP is a protocol designed to replace the older WHOIS system for querying databases that store the registered users or assignees of internet resources, such as domain names and IP addresses. It provides more structured data and supports internationalization and access control rules.

Key Features of RDAP:

  • Structured Data: RDAP returns data in a structured format (JSON), which is easier for applications to parse and use.
  • Security and Access Management: RDAP includes features to handle access control and potentially offer authenticated access, giving more control over who can see what data.
  • Standardization and Compliance: It is a standardized protocol developed and managed by the Internet Engineering Task Force (IETF).

Usage Policies and Accessibility:

While RDAP itself is free to use, the extent of data provided and the rate limits on requests can depend on the specific RDAP server being queried. Different registries and registrars implement their own RDAP servers, and they may have specific policies regarding how frequently you can make requests:

  • Public RDAP Servers: Many internet registries like ICANN for domain names, ARIN for North American IP addresses, and other regional internet registries provide RDAP services that are publicly accessible.
  • Rate Limiting: While the service is free, like any server-based service, RDAP servers may implement rate limiting to prevent abuse and ensure server stability.

Considerations:

  • For Bulk Queries: If you need to perform a large number of queries regularly, you might still face practical limitations like rate limiting. In such cases, reaching out to the registry or registrar for potential API keys or increased access might be necessary.
  • Compliance and Privacy: RDAP servers are also more likely to comply with modern data privacy laws by restricting access to personal data compared to traditional WHOIS services.

How to Access RDAP:

Using RDAP generally involves making HTTP requests to an RDAP server URL. Here’s a basic example of how you might query RDAP data using curl from the command line:

curl https://rdap.bootstrap.fashion/rdap/domain/example.com

This example assumes you are querying a hypothetical RDAP server (rdap.bootstrap.fashion) for information about example.com. Replace the URL with the actual RDAP server URL of the domain or IP address registry you are interested in.

In summary, RDAP provides a modern, standardized, and structured way to access registration data freely, but practical usage might be subject to certain limitations depending on the server’s policies.

Conclusion

Choosing a commercial WHOIS API service can mitigate the limitations of traditional WHOIS, especially if you need high volumes of queries for a business or research purpose. It’s important to review each service’s specific features, costs, and policies to find the best fit for your needs.

Leave a Reply

Your email address will not be published. Required fields are marked *