NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page A whois class (sort of)

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 01-14-2008, 02:29 AM THREAD STARTER               #1 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




A whois class (sort of)


This isn't actually a whois class, it's a class that selects a whois server for a domain. If multiple whois servers are available, it chooses a random one.
The script uses this XML file to get the servers (human-readable version here).
As per their request, by default the class keeps a local cache of the XML file and updates once a day. To enable caching, make the directory the cache will be in writable. To disable caching change
PHP Code:
var $use_cache true
To
PHP Code:
var $use_cache false
Here is the class
PHP Code:
class whoisserver{

    var 
$url "http://serverlist.domaininformation.de/";
    var 
$cache "whois.xml";
    var 
$use_cache true;

    function 
parsexml($file){
        
$parser xml_parser_create('');
        
xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
        
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
        
xml_parse_into_struct($parserfile_get_contents($file), $tags);
        
xml_parser_free($parser);
    
        
$elements = array();
        
$stack = array();
        foreach(
$tags as $tag){
            
$index count($elements);
            if(
$tag['type'] == "complete" || $tag['type'] == "open"){
????: NamePros.com http://www.namepros.com/code/418136-a-whois-class-sort-of.html
                
$elements[$index] = array();
                
$elements[$index]['name'] = $tag['tag'];
                
$elements[$index]['attributes'] = empty($tag['attributes']) ? "" $tag['attributes'];
                
$elements[$index]['content']    = empty($tag['value']) ? "" $tag['value'];
            
                if(
$tag['type'] == "open"){    # push
                    
$elements[$index]['children'] = array();
                    
$stack[count($stack)] = &$elements;
                    
$elements = &$elements[$index]['children'];
                }
            }
        
            if(
$tag['type'] == "close"){    # pop
                
$elements = &$stack[count($stack) - 1];
                unset(
$stack[count($stack) - 1]);
            }
        }
        return 
$elements[0];
    }

    function 
cachecheck(){
        if(!
file_exists($this->cache) || filemtime($this->cache) < time() - 86400 && $this->use_cache == true){
            
$xml file_get_contents($this->url);
            
            
unlink($this->cache);
            
            
$fp fopen($this->cache"w+");
            
fwrite($fp$xml);
            
fclose($fp);
            
            return 
true;
        }else{
            return 
false;
        }
    }

    function 
getxml(){
        
$file = ($this->use_cache == true) ? $this->cache $this->url;
        
$xml $this->parsexml($file);
        
        return 
$xml;
    }
    
    function 
getservers(){
        
$xml $this->getxml();
        
$servers = array();

        foreach(
$xml['children'] as $child){
            if(!empty(
$child['children'])){
                foreach(
$child['children'] as $baby){
                    if(
$baby['name'] == 'domain'){
                        if(empty(
$servers["{$baby['attributes']['name']}"])){
                            
$servers["{$baby['attributes']['name']}"] = array("{$child['attributes']['host']}");
                        }else{
                            
$servers["{$baby['attributes']['name']}"][] = "{$child['attributes']['host']}";
                        }
                    }
                }
            }
        }
        
        return 
$servers;
    }
    
    function 
pickserver($tld){
        
$servers $this->getservers();
        
        if(!empty(
$servers["{$tld}"])){
            
$server $servers["{$tld}"];
            if(
count($server) != 1){
????: NamePros.com http://www.namepros.com/showthread.php?t=418136
                return 
$server[array_rand($server)];
            }else{
                return 
$server[0];
            }
        }else{
            return 
false;
        }
    }
    
    function 
go($domain){
        
$this->cachecheck();
        
        
$domain str_replace('www.'''$domain);
        
$domain strrev($domain);
        
$domain explode('.'$domain);
        
        if(
count($domain) == 3){
            
$tld strrev($domain[1]) . '.' strrev($domain[0]);
        }else{
            
$tld strrev($domain[0]);
        }
        
        
$server $this->pickserver($tld);
        return 
$server;
    }

I wrote the whole class except the parsexml() function which I found on php.net. Do what you want with the code.

Here are two examples:
PHP Code:
$ws = new whoisserver;
echo 
$ws->go("http://www.yarrt.com")."\n"// should return "whois.crsnic.net"
echo $ws->go("http://hello.uyp")."\n"// should return false 
Barrucadu is offline  
Old 01-14-2008, 09:19 PM   #2 (permalink)
Resistance is Futile
 
Kadenz's Avatar
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 1,094
Kadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to all
 



Wildlife Lou Gehrig's Disease (ALS)
Very nice class Mikor, could come in very handy. I may have to use that in the Web 2.0 Whois checker i'm working on. (Don't worry, i'll credit you. )
Can't rep you, apparently i'm too fond of you
__________________
Freelance Web Developer
PHP, MySQL, XHTML, CSS, Javascript, jQuery, Wordpress
Portfolio: www.bundy.ca
Kadenz is offline  
Old 01-15-2008, 04:16 AM   #3 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Providing http://serverlist.domaininformation.de/ is kept up to date that could come in very handy.

1 addition that might be useful however is to store the file locally and only retrieve it say once a day instead of every time the class is used.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 01-15-2008, 05:06 AM THREAD STARTER               #4 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Originally Posted by peter@flexiwebhost
1 addition that might be useful however is to store the file locally and only retrieve it say once a day instead of every time the class is used.
It does, have another look at the code.
Barrucadu is offline  
Old 01-15-2008, 05:15 AM   #5 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Next time I demand you forget that so I don't look silly when I make the suggestion

Ooh and read your pm. Sorry I forgot I will read the post in a minute and get back to you .
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 01-15-2008, 05:46 AM THREAD STARTER               #6 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Originally Posted by peter@flexiwebhost
Next time I demand you forget that so I don't look silly when I make the suggestion
Forget what? My mind is suddenly blank

Originally Posted by peter@flexiwebhost
Ooh and read your pm. Sorry I forgot I will read the post in a minute and get back to you .
Good, and if you're likely to forget again, you may find it easier to remember if you open it in another tab - it's very difficult to forget things that are staring you in the face.
Barrucadu is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 03:28 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger