Oct 26th

3

Domain name WHOIS query using PHP

5 years 4 weeks ago comment 3 Comments

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:

class WhoisQuery {

	var $whoisServer;
	var $availableString;
	var $domainName;
	var $extension;

	function checkAvailability() {

		$output = "";

		if(($connection = @fsockopen($this->whoisServer, 43, $errno, $errstr, 15)) == false){
			return array("errno" => $errno, "errstr" => $errstr);
		}

		fputs($connection, $this->domainName . $this->extension . "\r\n");

		while(!feof($connection)) {
			$output .= fgets($connection, 4096);
		}

		fclose($connection);

		if (strlen($output) == 0) {
			return array("errno" => 0, "errstr" => "no output");
		}

		if ( is_integer( strpos($output, $this->availableString) ) ) {
			return true;
		} else {
			return false;
		}

	}

}

You can use it as shown below:

$domainAvailability = new WhoisQuery;
$domainAvailability->whoisServer = "whois.verisign-grs.com";
$domainAvailability->availableString = "No match for";
$domainAvailability->domainName = "ankur";
$domainAvailability->extension = ".com";

$result = $domainAvailability->checkAvailability();

if ($result === true) {
	echo "available";
} elseif ($result === false) {
	echo "not available";
} else {
	echo $result["errstr"];
}

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

Leave a Reply