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:

whois google.com

On most Linux installs and see some information about the google.com 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 www.iana.org/domains/root/db/
 $servers = array(
  "biz" => "whois.neulevel.biz",
  "com" => "whois.internic.net",
  "us" => "whois.nic.us",
  "coop" => "whois.nic.coop",
  "info" => "whois.nic.info",
  "name" => "whois.nic.name",
  "net" => "whois.internic.net",
  "gov" => "whois.nic.gov",
  "edu" => "whois.internic.net",
  "mil" => "rs.internic.net",
  "int" => "whois.iana.org",
  "ac" => "whois.nic.ac",
  "ae" => "whois.uaenic.ae",
  "at" => "whois.ripe.net",
  "au" => "whois.aunic.net",
  "be" => "whois.dns.be",
  "bg" => "whois.ripe.net",
  "br" => "whois.registro.br",
  "bz" => "whois.belizenic.bz",
  "ca" => "whois.cira.ca",
  "cc" => "whois.nic.cc",
  "ch" => "whois.nic.ch",
  "cl" => "whois.nic.cl",
  "cn" => "whois.cnnic.net.cn",
  "cz" => "whois.nic.cz",
  "de" => "whois.nic.de",
  "fr" => "whois.nic.fr",
  "hu" => "whois.nic.hu",
  "ie" => "whois.domainregistry.ie",
  "il" => "whois.isoc.org.il",
  "in" => "whois.ncst.ernet.in",
  "ir" => "whois.nic.ir",
  "mc" => "whois.ripe.net",
  "to" => "whois.tonic.to",
  "tv" => "whois.tv",
  "ru" => "whois.ripn.net",
  "org" => "whois.pir.org",
  "aero" => "whois.information.aero",
  "nl" => "whois.domain-registry.nl",
  "uk" => "whois.nic.uk",
  "us" => "whois.nic.us",
  "travel" => "whois.nic.travel",
  "gov" => "whois.dotgov.gov",
  "it" => "whois.nic.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);
  }
  fclose($conn);
 } 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('google.com').'</pre>';

 

Comments

thanks
Permalink

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.

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
5 + 8 =
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.