≡ Menu

How to do WHOIS lookups using PHP? With Examples using PHPWhois…

How to perform domain name and IP address WHOIS lookups using PHP? Is there a fast and reliable library that can be used to do whois database lookups?

I use the library PHPWhois very frequently to perform WHOIS database lookups. The library is very reliable and quite fast. It can do Whois lookups of both domain names and IP addresses.

In this short tutorial, I will show using examples on how to use PHPWhois:

Downloading and Installing PHPWhois

To install PHPWhois, you need to download the module from this source forge page.

Unzip the archive using your favorite compression program (e.g. PowerArchiver) into a directory on your web server.

Say the directory is:

/home/ewhathow/public_html/whois/

The directory structure is quite flat and the files are located in the directory you decompressed the files in.

Doing domain name Whois lookups using PHPWhois:

The following example snippet of PHP code will show you how to do whois lookups using PHPWhois:

require_once('/home/ewhathow/public_html/phpwhois/whois.main.php');
$whois = new Whois();
$query = "google.com";
$whois->deep_whois=TRUE;
$result = $whois->Lookup($query, false);

$regrinfo = $result['regrinfo'];
$regyinfo = $result['regyinfo'];
$owner = $regrinfo['owner'];
$admin = $regrinfo['admin'];
$tech = $regrinfo['tech'];
$owneremail = $owner['email'];
$techemail = $tech['email'];
$adminemail = $admin['email'];

$rawdata = $result['rawdata'];

/* The following three fields are returned when deep_whois=FALSE (or TRUE) */
$regyinfo = $result['regyinfo'];
$registrar = $regyinfo['registrar'];
$regurl = $regyinfo['referrer'];
			
print_r($result);

The first line creates a new WHOIS object which is used to query the WHOIS database. I will explain this demo code in detail. The code gets the owner, admin and tech email addresses of the domain name google.com. You can replace it with your own domain name, or write a function that gets passed a domain name or an IP address.

The line that says deep_whois=TRUE instructs that PHPWhois should do a deep whois by contacting the authoritative whois servers of a domain name (usually provided by the domain name registrar). A deep whois will populate all the fields of the whois record including the mailing address, email address, names of the owner, tech and admin contacts of the domain name. If you just want to check whether the domain name is registered or not, or just get the registrar of the domain name, you can choose to use deep_whois=FALSE. This will just return the registrar name, and the registrar url of the domain name.

The next set of lines get the owner, tech and admin emails of the domain name. The $result array is a multi-level array and you need to extract each individual array to get to the actual fields. For instance, first you need to get the $regrinfo sub-array from which you extract the $owner, $admin and $tech arrays. These three arrays contain the information about the owner, admin and tech contacts of a domain name or IP address.

The best way to understand the $result multi-dimensional array is to do a print_r($result) on it. The statement print_r($result) will print the whole array including all sub-arrays with the keys that can be used to access them.

If you need the raw data that the WHOIS server returns, you should use $result['rawdata']. The raw data is a full text answer that the WHOIS server returned including their terms of use. PHPWhois parses this raw data and populates the $result multi-level array.

Doing IP Address Whois lookups using PHPWHois

Doing IP address lookups is quite simple. Just replace your domain name with your IP address:

The following example PHP code will show you how to get the organization that owns the IP address:

$ipaddress = "8.8.8.8";
$whois = new Whois();
$whoisresult = $whois->Lookup($ipaddress, false);

print_r($whoisresult);

// Get the owner of the IP address from the array
if(is_array($whoisresult))
{
	if(is_array($whoisresult['regrinfo']))
	{
		if(is_array($whoisresult['regrinfo']['owner']))
		{
			$company = $whoisresult['regrinfo']['owner']['organization'];
		}
	}
}

Again, to understand the output multi-dimensional array, you should do a print_r on the returned array. As an example, I have shown in the above PHP script how to get the organization that owns the IP address. This is the following field:

$whoisresult['regrinfo']['owner']['organization'];

Caching the Results

I just wanted to quickly mention that it is very important that you cache the results of the WHOIS lookups be it for a domain name or for an IP address. This is because most WHOIS servers have a daily cap on how many public WHOIS requests can come from an IP address (the address of the server from which you are running your PHP scripts). If you do Whois requests excessively, the company may ban your IP and you may have to contact the company to get access again. This is true for Whois servers of both domain names and IP addresses. Sometimes, if you send too many Whois requests, the server may decide to send only the owner name and the organization associated with the domain name and you may lose access to the deep whois data that you may otherwise get.

So that was a quick tutorial on how to do Whois lookups using PHPWhois. I also gave you several examples on how to interpret the output array that PHPWhois returns. Remember that if you don’t understand the structure of an array, the best way to interpret it is to use the print_r statement! I hope this article was useful to you!

{ 0 comments… add one }

Leave a Comment