4

How to get domain expires date using whois response data on php ?

Normally i use whois response for check domain availability.

eg: check DRGDRGDRGRGDRG.COM if whois response data have word No match for it's mean this domain is available.

No match for domain "DRGDRGDRGRGDRG.COM".

But now i want to check domain expires date using whois response data like this

Domain Name: GOOGLE.COM
Registrar: MARKMONITOR INC.
Sponsoring Registrar IANA ID: 292
Whois Server: whois.markmonitor.com
Referral URL: http://www.markmonitor.com
Name Server: NS1.GOOGLE.COM
Name Server: NS2.GOOGLE.COM
Name Server: NS3.GOOGLE.COM
Name Server: NS4.GOOGLE.COM
Status: clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited 
Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited 
Status: clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited 
Status: serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited 
Status: serverTransferProhibited https://icann.org/epp#serverTransferProhibited 
Status: serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited 
Updated Date: 20-jul-2011
Creation Date: 15-sep-1997
Expiration Date: 14-sep-2020

How can i do that ? thank you

mongmong seesee
  • 987
  • 1
  • 13
  • 24

1 Answers1

1

This is not a simple question of parsing the response string as it might sound at the first moment because domain name registrars provide information in different format.

I guess we have two options here:

  1. Use some library and when it fails parse the string
  2. Do everything yourself, do not use any library and parse the output.

I suggest to start with some library but I really do not know the 'perfect' one. I'm going to try phpWhois. If it fails it gives raw data to try and parse it on our own.

First you need to install the library. I do this using Composer. Below is my composer.json file

{
  "require": {
    "phpwhois/phpwhois":"dev-master",
    "mso/idna-convert": "0.9.1"
  }
}

Note that latest phpWhois version does not work with latest idna-convert version, this is why I have to specify it in my requirements.

Execute composer install to download the libraries.

And finally PHP script to query the domain:

<?php

require(__DIR__ . '/vendor/autoload.php');

use phpWhois\Whois;

$whois = new Whois();
$whois->deepWhois = true;

$query = isset($argv[1]) ? $argv[1] : 'google.com';
$result = $whois->lookup($query);

$registered = isset($result['regrinfo']['registered']) && $result['regrinfo']['registered'] == 'yes';
if (!$registered) {
    echo 'Domain: '.$query.' not registered.'.PHP_EOL;
} else {
    if (isset($result['regrinfo']['domain']['expires'])) {
        echo 'Domain: '.$query.PHP_EOL;
        echo 'Expired: '.$result['regrinfo']['domain']['expires'].PHP_EOL;
    } else {
        echo 'Domain: '.$query.PHP_EOL;
        echo 'Trying to find expires date...'.PHP_EOL;
        foreach ($result['rawdata'] as $raw) {
            if (strpos($raw, 'Expiry Date:') !== false) {
                echo 'Expired: '.trim(explode(':', $raw)[1]).PHP_EOL;
            }
        }
    }
}

It takes domain name as the first script argument $argv[1].

If the library does not parse the results from registrar we try to parse it manually. I've added a simple check

if (strpos($raw, 'Expiry Date:') !== false) {
    echo 'Expired: '.trim(explode(':', $raw)[1]).PHP_EOL;
}

You might search for 'Expiration Date' in the raw response data or do some better logic based on your experience with parsing data if you will. Perfectly the library should do this but sometimes it fails.

Victor Smirnov
  • 3,450
  • 4
  • 30
  • 49