Oct 26th


Domain name WHOIS query using PHP


This post is about a PHP class which allows you to check if a domain name is available or not using WHOIS servers. The class simply opens a socket connection to the WHOIS server and passes the domain name to check for. Based on the available string you provide, it checks if the domain name is available or not.

The PHP code is below:

01 class WhoisQuery {
03     var $whoisServer;
04     var $availableString;
05     var $domainName;
06     var $extension;
08     function checkAvailability() {
10         $output = "";
12         if(($connection = @fsockopen($this->whoisServer, 43, $errno, $errstr, 15)) == false){
13             return array("errno" => $errno, "errstr" => $errstr);
14         }
16         fputs($connection, $this->domainName . $this->extension . "\r\n");
18         while(!feof($connection)) {
19             $output .= fgets($connection, 4096);
20         }
22         fclose($connection);
24         if (strlen($output) == 0) {
25             return array("errno" => 0, "errstr" => "no output");
26         }
28         if ( is_integer( strpos($output, $this->availableString) ) ) {
29             return true;
30         } else {
31             return false;
32         }
34     }
36 }

You can use it as shown below:

01 $domainAvailability = new WhoisQuery;
02 $domainAvailability->whoisServer = "whois.verisign-grs.com";
03 $domainAvailability->availableString = "No match for";
04 $domainAvailability->domainName = "ankur";
05 $domainAvailability->extension = ".com";
07 $result = $domainAvailability->checkAvailability();
09 if ($result === true) {
10     echo "available";
11 } elseif ($result === false) {
12     echo "not available";
13 } else {
14     echo $result["errstr"];
15 }

We pass it the WHOIS server in whoisServer, the string that should be present in the response if the domain name is available in availableString, the first part of the domain name in domainName and the extension in extension. $result is an array with keys errno and errstr if an error occurred, true if the domain name is available and false if it isn’t.  Please note that WHOIS servers may limit the frequency of queries you perform. This has not been handled in the class.

You can connect to a WHOIS server manually using a program like PuTTY . Enter the WHOIS server name in the Host Name section and use the port 43. Type in the full domain name (example, ankur.com) and the response will be the WHOIS data.

Now, how do you find WHOIS servers for the extension you would like to check ? whois-servers.net provides some aliases for whois servers. For example, use com.whois-servers.net to connect to the .com WHOIS server. You can also check the IANA’s Database of Top Level Domains to see if it contains WHOIS server information for your extension.

Related posts:

  1. Resume HTTP downloads in PHP using cURL or fsockopen
  2. cURL wrapper class with executable and PHP extension support

3 Responses to “Domain name WHOIS query using PHP”

  1. anver says:

    This is a wonderful class. Is it possible to rewrite this script so that it uses cURL instead of fsockopen ?

    Thanks…. Great work, Keep it up

    • Hi Anver,

      Using cURL, you would be able to access HTTP(S) and FTP sites, but as far as I know, there is no way to connect to a WHOIS server on port 43 using cURL.

      The PHP manual for cURL (http://www.php.net/manual/en/intro.curl.php) does say that it supports Telnet though.
      I’m not sure if we might somehow be able to use that.
      I suspect we still won’t be able to use that, but maybe I’ll try it out sometime and reply back.


      • anver says:

        Hi Ankur,
        Thanks for the quick reply. I used this code to query the server and i get the output from the server, but the i think the post variables i’m sending to the server is not correct. This is the curl code i use to query the server.

        Please download the class from here. I’ve also attached the output i get from the server when i use curl.


        Please remove the above url from the comment, also please send me a copy of your reply to my email too.. :)