Free, tested & ready to use examples!
AnyExample.com
 
Web anyexample.com
 

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($server43$ae_whois_errno$ae_whois_errstrAE_WHOIS_TIMEOUT);
        if (!
    $f
            return 
    false// connection failed 

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

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

        
    // 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');
    ?>

    whois.verisign-grs.com is a whois server for .com domains (as of september 2007)

     

    tested 
  • Mac OS X 10.4.10 :: PHP 5.2.4
  • Windows 2003 server :: PHP 4.4.5
  •  


     
    © AnyExample 2010-2013
    License | Privacy | Contact