CakePHP WhoIs Component
Published: on 10/9/08 | Comments (0)
Here's one for all you aspiring web hosting ressellers out there, a fast and flexible whois class all wrapped up as a CakePHP component.
The component makes use of the excellent phpWhoIs class which you will need to download, unzip and place in APP/vendors/phpwhois in order for the component to work.
Click here to download phpWhoIs
phpWhoIs - a brief description
This package contains a Whois (RFC954) library for PHP. It allows a PHP program to create a Whois object, and obtain the output of a whois query with the Lookup function.
The response is an array containing, at least, an element 'rawdata', containing the raw output from the whois request.
In addition, if the domain belongs to a registrar for which a special handler exists, the special handler will parse the output and make additional elements available in the response. The keys of these additional elements are described in the file HANDLERS.
It fully supports IDNA (internationalized) domains names as defined in RFC3490, RFC3491, RFC3492 and RFC3454.
It also supports ip/AS whois queries which are very useful to trace SPAM. You just only need to pass the doted quad ip address or the AS (Autonomus System) handle instead of the domain name. Limited, non-recursive support for Referral Whois (RFC 1714/2167) is also provided.
The WhoIs Component
OK, if you have been following so far, you'll have downloaded phpWhoIs, unzipped it and placed all the files in a folder called phpwhois inside either your APP/vendors folder or your shared vendors folder, now to integrate it into the CakePHP framework using a component.
APP/controllers/components/whois.php
<?php
class WhoisComponent extends Object {
function lookup($domain,$deep_whois = true) {
App::import('Vendor','whois',array('file'=>'phpwhois'.DS.'whois.main.php'));
$whois = new Whois();
$whois->deep_whois = $deep_whois;
return $whois->Lookup($domain,false);
}
}
?>
Short and simple and does the job, basically all we are doing here is creating a wrapper to access the phpwhois class and it's lookup method.
In use
OK, you've got the phpwhois package and the component, now to use them within a controller, simply add Whois to your components array
var $components = array('Whois');
Then from within any of your actions you can simply use:
$result = $this->Whois->lookup('example.com'));
Faster results
The whois lookup is pretty fast already, but if you don't want all the extended information and are simply interested in seeing if a domain is registered or not, then pass false as a second parameter to the lookup method.
Possible uses
Beside the standard whois query or domain registration check, you can also use this to process env('REMOTE_ADDR') to give you information about the current visitors ISP, icluding country code and ISP name, which could be pretty usefull within a stats system or for auto detecting local currency. Let me know if you come up with any other uses or if you use this code in a site then let me know and I will give you a mention.
Anyway, that's your lot for today, till the next time
