PHP whois client function

abstract 
This article contains PHP implementation of whois client (as a function ae_whois), which may be used to request domain information from specified whois servers
compatible 
  • PHP 4.0 or higher
  • PHP 5 or higher

Whois protocol (defined by RFC 3912) is probably one of the simplest internet protocols.

In fact, whois client should open TCP connection to port 43, send query, send CR-LF ("\r\n" constant) and then receive response. Here is the source code:

source code: php
<?php 
function ae_whois($query, $server)
{

    
define('AE_WHOIS_TIMEOUT', 15); // connection timeout
    
global $ae_whois_errno, $ae_whois_errstr;

    
// connecting
    
$f = fsockopen($server, 43, $ae_whois_errno, $ae_whois_errstr, AE_WHOIS_TIMEOUT);
    if (!
$f)
        return
false; // connection failed

    // sending query    
    
fwrite($f, $query."\r\n");

    
// receving response
    
$response = '';
    while (!
feof($f))
        
$response .= fgets($f, 1024);

    
// closing connection
    
fclose($f);

    return
$response;
}
?>

As you can see, function takes two arguments: whois query and whois server. Function returns server response (if everything was fine), or 'false' constant(that means connection failed). fsockopen error code and error string are written to global variables $ae_whois_errno and $ae_whois_errstr.

You can also change connection timeout by modifying AE_WHOIS_TIMEOUT constant.

Usage example: getting information about domain 'iphone.com'

source code: php
<?php 

// copy-paste function ae_whois(see above) here

echo ae_whois('iphone.com', 'whois.verisign-grs.com');
?>


태초에 나는 개그이야기를 만들었다.
내말을 믿고 나를 따르면 천당,
내말을 믿지않고 나를 따르지 않으면 지옥,
나는 하늘나라(우주)에 사느니라.

그럼 난 외계인?