Content Notice!

This post is really old, and no longer reflect my skill level, views or opinions, it is made available here for archival purposes (it was originally on my old WordPress blog).

Keep that in mind when you read the contents within.

How To Query WHOIS Servers With PHP

What is WHOIS?

The WHOIS protocol is a protocol that is used for querying domain information from various WHOIS Servers.

It typically returns a set of information about the domain, like registration and expiration date, as well as some information about the person that registered the domain name, like  their name, email, phone number and address.

How WHOIS Works

The WHOIS protocol is so dead simple that even I managed to do it. The official RFC specification ( RFC 3912 ) pretty much says that all you have to do to query a WHOIS server is to connect to a WHOIS server on the  port 43.

Then send this piece of data "domain.com <CR><LF>".

You might be wondering what is meant by  <CR><LF>, It's quite simple really, it means Carriage Return and Line Feed ( More Information ),  in PHP this is the equivalent to "\r\ " without the quotes.

That might have been hard to follow, here is a simpler step by step version:

1. Connect to WHOIS server on port 43. 2. Send "domain.com <CR><LF>". 3. Receive information and Close connection.

Now the question, how do we do this in code?

How to Query WHOIS in PHP

It's quite easy to be honest, here is all we need. Note: For simplicity, this code does not have ANY error checking whatsoever.

<?php

// Open a Socket connection to our WHOIS server
$fp = fsockopen("whois.verisign-grs.com", 43);

// The data we're sending
$out = "helgetech.com\r\n";

// Send the data
fwrite($fp, $out);

// Listen for data and "append" all the bits of information to
// our result variable until the data stream is finished
// Simple: "give me all the data and tell me when you've reached the end"
while (!feof($fp)) {
	$whois .= fgets($fp, 128);
}

// Print out the data we've received
echo "<pre>";
echo $whois;
echo "</pre>";

// Close the Socket Connection
fclose($fp);

?>

First we need to establish a socket connection to our WHOIS server, we do this by using the function  fsockopen(hostname, port)

$fp = fsockopen("whois.verisign-grs.com", 43);

We're going to use it's two first parameters that represents the hostname for the WHOIS server and the port number, for .com domains you can use whois.verisign-grs.com as the WHOIS server, the port should always 43.

If you wan't to check a different TLD( .info for instance), please reference the IANA database (click on a TLD, scroll to the bottom, the WHOIS server is listed there).

By setting the function as the value of a variable we're storing it's file handle, this is pretty much an Object that we can use to reference the socket connection later in our code.

Now we need to set the data that we want to send to the WHOIS server.

// The data we're sending $out = "helgetech.com\r\ ";

We assign our $out  variable the value helgetech.com followed by \r\ characters (same as  <CR><LF> )   Now we want to send this data, we do this by using the built-in PHP function fwrite($filehandle, $data)  like this:

// Send the data fwrite($fp, $out);

The $fh  variable is called a file handle, as I wrote above this is an object used to reference our socket connections, in this context, we're sending data on that socket connection.

The second parameter is the data we want to send, which is "helgetech.com\r\ " .

Now for something slightly more confusing, we need to start listening for the response from the WHOIS server.

What we're gonna do is to listen for data from the file handle(socket connection) until it doesn't receive anything anymore.

// Listen for data and "append" all the bits of information to // our result variable until the data stream is finished // Simple: "give me all the data and tell me when you've reached the end"

while (!feof($fp)) {
	$whois .= fgets($fp, 128);
}

The feof($fh)  function checks for a "end of file" in a file pointer(or in our case a socket connection), an end of file
is pretty much just the end of the file or stream.

So we will listen for data until the data stops coming, pretty much.

In the while loop we are appending the data we receive to a variable called
$whois  by appending the returning value from the function fgets($filehandle, $length) ,  according to the PHP
documentation, this function gets a line from a filehandle at a certain length specified by the second parameter.

This means that every time our while loop loops, the function it will grab and return the next 128 bytes of our socket
connection response and append it to our $whois  variable.

When the while loop reaches the end of the socket stream, it will stop running and continue on with our code, this is
what we do next:

// Print out the data we've received echo "<pre>"; echo $whois; echo "</pre>";

We want to print out the variable $whois  and we're going to wrap it in some
[<pre> tags](http://www.w3schools.com/tags/tag_pre.asp) so we can see it properly formatted on our page. Lastly we have
to close our socket connection:

// Close the Socket Connection fclose($fp);

the function fclose($filehandle)  will close a socket or file that is connected to the $filehandle, This is to avoid
having rogue connections stealing memory and various other nasty things.

## Try it yourself

And that is pretty much it, Now you've got your very own WHOIS script made in PHP, I hope you try to extend it and
improve upon the foundation that you've learned.

You can save the code in a .php file and upload it to your web host( Check
out [WebFaction](http://www.webfaction.com/signup?affiliate=helgesverre "Hosting for Developers"), read my
[MDDHosting review](https://helgesverre.com/blog/mddhosting-review/ "MDDHosting Review") or learn
[how to host your site for free](http://helgesverre.com/blog/how-to-host-your-website-for-free-000webhost/ "How to Host Your Website For Free")
if you don't have a web host yet.) or run it locally with software like
[WAMP](http://www.wampserver.com/en/), [XAMPP](https://www.apachefriends.org/index.html) or whichever local web server
software you prefer.