Oct 26th

3

Domain name WHOIS query using PHP

PHPWHOIS

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 {
02  
03     var $whoisServer;
04     var $availableString;
05     var $domainName;
06     var $extension;
07  
08     function checkAvailability() {
09  
10         $output = "";
11  
12         if(($connection = @fsockopen($this->whoisServer, 43, $errno, $errstr, 15)) == false){
13             return array("errno" => $errno, "errstr" => $errstr);
14         }
15  
16         fputs($connection, $this->domainName . $this->extension . "\r\n");
17  
18         while(!feof($connection)) {
19             $output .= fgets($connection, 4096);
20         }
21  
22         fclose($connection);
23  
24         if (strlen($output) == 0) {
25             return array("errno" => 0, "errstr" => "no output");
26         }
27  
28         if ( is_integer( strpos($output, $this->availableString) ) ) {
29             return true;
30         } else {
31             return false;
32         }
33  
34     }
35  
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";
06  
07 $result = $domainAvailability->checkAvailability();
08  
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:

    Hi,
    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.

      Ankur

      • 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.

        http://www.smaartweb.com/works/toankur.zip

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

        Thanks….