Robust Domain Whois Query In PHP

A whois query will tell you some information about a domain name. Although not available as a default on Windows systems you can type:


On most Linux installs and see some information about the domain. What information you see depends on the domain you are looking at and the rules that the Top Level Domain (TLD) employs. For more information on whois you can take a look at the Wikipedia entry on the subject.

In order to get this information in PHP you will need to ask the appropriate TLD whois server for information about the domain. But where do you get a list of the servers? Well the root zone database has a comprehensive list of domains, and by clicking on the TLD links you can see whois information at the bottom of the page.

With the list of servers in hand we can now write a function that does a whois lookup. The first thing we need to do is tidy up the domain name to get rid of any information (like http and www) that whois doesn't need and then find the TLD which will tell us what whois server we need to ask. The next step is to open a connection to fsockopen() call to the whois server name on port 43 and then use the fwrite() function to send the domain name we want to know about.

function whois_query($domain)
 // fix the domain name:
 $domain = strtolower(trim($domain));
 $domain = preg_replace('/^http:\/\//i', '', $domain);
 $domain = preg_replace('/^www\./i', '', $domain);
 $domain = explode('/', $domain);
 $domain = trim($domain[0]);
 // split the TLD from domain name
 $_domain = explode('.', $domain);
 $lst = count($_domain)-1;
 $ext = $_domain[$lst];
 // the list of whois servers
 // most taken from
 $servers = array(
  "biz" => "",
  "com" => "",
  "us" => "",
  "coop" => "",
  "info" => "",
  "name" => "",
  "net" => "",
  "gov" => "",
  "edu" => "",
  "mil" => "",
  "int" => "",
  "ac" => "",
  "ae" => "",
  "at" => "",
  "au" => "",
  "be" => "",
  "bg" => "",
  "br" => "",
  "bz" => "",
  "ca" => "",
  "cc" => "",
  "ch" => "",
  "cl" => "",
  "cn" => "",
  "cz" => "",
  "de" => "",
  "fr" => "",
  "hu" => "",
  "ie" => "",
  "il" => "",
  "in" => "",
  "ir" => "",
  "mc" => "",
  "to" => "",
  "tv" => "",
  "ru" => "",
  "org" => "",
  "aero" => "",
  "nl" => "",
  "uk" => "",
  "us" => "",
  "travel" => "",
  "gov" => "",
  "it" => ""
 if (!isset($servers[$ext])) {
  die('Error: No matching whois server found!');
 $nic_server = $servers[$ext];
 $output = '';
 // connect to whois server:
 if ($conn = fsockopen($nic_server, 43)) {
  fwrite($conn, $domain."\r\n");
  while (!feof($conn)) {
   $output .= fgets($conn, 128);
 } else {
  die('Error: Could not connect to ' . $nic_server . '!');
 return $output;

If we can't connect to the server or no TLD server is found then the program simply exits. Note that this list isn't entirely complete, but it should suffice for 99% of englsh speaking websites.

To run the function just give it a domain name and print the output, if you want to preserve the white space then use pre tags.

echo '<pre>'.whois_query('').'</pre>';




So where would you start off? The first thing you certainly can do is begin incorporating neighborhood to your own search phrases. A very good case in point is if you are selling essential oils, your key words can possibly be"essential oils," or even"high quality essential oils." Now you want to introduce regional, so you can just turn your key words into a long-term keyword using place, such as"essential oils London" or even"essential oils in New York" as examples.


Add new comment

The content of this field is kept private and will not be shown publicly.
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.