HomePHP Page 5 - Using PHP classes to navigate distributed whois databases
Looking at the pieces - PHP
Whois services changed radically this year with the introductionof competition at the registrar level in the .com/.net/.orgnamespace and with it the advent of the SRS (Shared Registry System). Find out how to navigate the new whois in your PHP scripts.
The base class is class "Whois" defined in main.whois. We've intentionallyupper-cased the "W" which means that if can begin working with this in your existing scripts, the new class will not collide with anything thatmay be using the old version 1 whois.php3.
The $Query variable is a hash that functions more or less as the localenvironment for the object. It keeps track of things like what our current whois server is ($Query["server"]), what the current TLD is($Query["tld"], not as obvious as you might think at first glance, considering the nominet uk.com and gb.net, which we support),what the actual query is ($Query["string"]) and last but definatelynot least, an array of any errors encountered in $Query["errstr"].
We invoke the class in the normal method, and pass our query as well:
$whois = new Whois("devshed.com");
In PHP we can create constructors for our classes by putting a functionin the class with the same name as the class. We rely heavily on thosehere and in our first constructor we basically get everything readyfor a normal domain lookup (we don't have to invoke the class this waybut for our purposes we'll just look at domain lookups in normal fashionfor now).
So we set the TLD, set the current whois server, and if we have one,we define an extended handler. The whois.main then takes care of thebasic tasks that we need to do regardless of what we're doing it to: connect to the server with Connect(), send the query string and read back the output with Lookup(), and then, if we have an extended handler,pass the result to Process().
One of the first orders of business to to load the "servers.whois" file.This contains the $DATA array which is an array indexed by supported TLD's, the values of which are arrays with their corresponding whoisserver as their first value, and an optional "extended handler" asvalue 2. We want to keep all this in a seperate file because thisdata will take on an update history seperate from the code itself,as new whois servers are reported and (hopfully) energetic PHP codersprovide additional extended classes for various TLDs and registrars.
The extended handlers demonstrate PHP extended classes and inheritance.Because output varies wildy from server to server, we provide additionalprocessing of raw output via modular and extensible "handlers", which in themselves, can utilize even further handlers.
Take "devshed.com" as our example again. We know from using the informationin the $DATA array that it will be queried at whois.nsiregistry.net, thecentral registry server. We also see that there is an extended handler"gtld" specified for this TLD. So the Process() function is going toload the code in gtld.whois and then invoke an instance of the gtldclass which extends the whois class.
Once again there is a class constructor in class gltd, and we parse the
registry output in to key/value pairs. It also turns out that the registrar for devshed.com happens to be Network Solutions, and as it so happens, wehave yet another extended class "netsol" so we query that server, get theresults, and and hand it off to class "netsol" parse it into key/value pairs as well.
The gtld object uses it's inherited Process() and Lookup() methods to do the follow-up query to the appropriate whois server, and if that'sit, it would stop there, returning the raw output. But in this casethe gtld class does have a "netsol" handler to further process the data, so it hands off to it for parsing.
We can then add further handlers for ICANN accredited registrars by addingthem $REGISTRARS array of the gtld, much in the fashion we can add additionalccTLD handlers to the $DATA array.