Checking a domain's availability with PHP

    This post is part of the ”WHOIS in PHP: consuming the Robowhois API” series; here is a list of all the articles contained this series:

  1. A PHP library to retrieve WHOIS informations
  2. Retrieving raw WHOIS informations in PHP
  3. Checking a domain’s availability with PHP
  4. Y U NO access WHOIS informations in PHP?

It’s been a while I don’t blog about the Robowhois PHP client that I’m developing together with David so, since we recently released the 0.8.0 version I want to share with you what you can do with it now.

Checking a domain’s availability

The availability API is probably the greatest feature of the Robowhois webservice, letting you check for a domain’s availability with a simple, uniform HTTP request.

Checking if google.com is available
1
2
3
4
5
6
7
8
9
<?php

use Robowhois\Robowhois;

$robowhois = new Robowhois('INSERT-YOUR-API-KEY-HERE');

if ($robowhois->isAvailable('google.com')) {
  echo "pretty nice dream, uhm?";
}

The opposite thing is achieved using the ->isRegistered() method.

You can also retrieve an array as returned from the webservice, by doing:

1
2
3
4
5
6
7
8
9
10
11
<?php

use Robowhois\Robowhois;

$robowhois = new Robowhois('INSERT-YOUR-API-KEY-HERE');

if ($availability = $robowhois->whoisAvailability('google.com')) {
  echo $availability['available'];
  echo $availability['registered'];
  echo $availability['daystamp'];
}

Retrieve informations about your account

A must-have, since you should always check how many remaining credits you have, the account API lets you retrieve some of your personal data from your Robowhois.com account:

Calculating how many left credits you have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

use Robowhois\Robowhois;

$robowhois = new Robowhois('INSERT-YOUR-API-KEY-HERE');

try {
    $credits = $robowhois->whoisAccount()->getCreditsRemaining();

      if ($credits > 100) {
              echo "No problem fella!";
    } else {
              echo "Time to go shopping looking for new API calls, uhm?";
    }
} catch (Exception $e) {
    echo "The following error occurred: " . $e->getMessage();
}

Minor things

We also polished some code, refactored stuff and added some tests (unit and integration ones).

For instance, when using the record API, you can retrieve the daystamp of the response as DateTime object:

retrieving the daystamp as an object or a string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

use Robowhois\Robowhois;

$robowhois = new Robowhois('INSERT-YOUR-API-KEY-HERE');

if ($whois = $robowhois->whoisRecord('google.com')) {
  // returns a DateTime object
  echo $whois->getDaystamp();

  // formats the DateTime
  echo $whois->getDaystamp()->format('Y-m-d');

  // returns a string
  echo $whois->getDaystamp(true);
}

You can download the latest tag of the library (currently 0.8.0) and start using it: the README exhaustively explains what you can do with this small client, and some samples are provided under the sample directory.


In the mood for some more reading?

...or check the archives.