WHOIS class in php (modified version)

Source: Internet
Author: User
Tags chop
: This article mainly introduces the WHOIS class (modified version) in php. if you are interested in the PHP Tutorial, please refer to it. 1. simplified the code. (In fact, some unnecessary variable definitions are removed)
2. the information retrieved from INTERNIC is too simple. further query is performed based on the whois server in INTERNIC feedback. For example, YAHOO has more detailed information on whois.networksolutions.com.

Class whois {

Var $ use_cache = 1;
Var $ FROM_CACHE = 0;
Var $ cache_dir = "./"; // according to your system settings

Var $ port = 43;
Var $ maxlen= 100;

// If you want to retry automatically after the connection fails,
// Set the number of retries $ MAX_RETRIES
Var $ MAX_RETRIES = 0;
Var $ SLEEP_VAL = 1;
Var $ RETRY = 0;

Var $ FOUND = 0; // no results for the query, and the number of times is 0
Var $ ERROR = 0; // number of errors in the query process
Var $ DATA_MIN = 8; // we should get at least 8 bytes of data.
Var $ DATA_COUNT = 0;

Var $ WHOIS_SERVER;
Var $ NEW_WHOIS;
Var $ FURTHER_INFO = 0;


// Open the SOCKET connection with WHOIS SERVER
// The default value is whois.internic.net.
Function connect ($ server ){
$ This-> RETRY = 0;
While ($ this-> RETRY <= $ this-> MAX_RETRIES ):
$ Ptr = fsockopen ($ server, $ this-> port );
If ($ ptr> 0 ):
$ This-> ERROR = 0; // just in case we're on a retry
Return ($ ptr );
Else:
$ This-> ERROR ++;
$ This-> RETRY ++;
Sleep ($ this-> SLEEP_VAL );
Endif;
Endwhile;
}

// Obtain simple query results and put them into an array in the unit of behavior
// Query international domain names
Function rawlookup ($ query, $ server ){

If (! $ Query ):
Return ("");
Endif;

$ Ptr = $ this-> connect ($ server );

If ($ ptr ):
If (! Ereg ($ query, "n $ ")):
$ Query. = "n ";
Endif;
Fputs ($ ptr, "$ query ");
$ I = 0;
$ This-> FOUND = 1;
While (! Feof ($ ptr )):
$ Array [$ I] = fgets ($ ptr, $ this-> MAXLEN );
$ This-> DATA_COUNT + = strlen (chop ($ array [$ I]);
If (eregi ("No match for", $ array [$ I]) | eregi ("No entries found", $ array [$ I]):
$ This-> FOUND = 0;
Elseif (eregi ("WHOIS database is down", $ array [$ I]):
$ This-> ERROR ++;
$ This-> FOUND = 0;
Elseif (eregi ("Please wait a while and try again", $ array [$ I]):
$ This-> ERROR ++;
$ This-> FOUND = 0;
Break;
Endif;
If (eregi ("Whois Server:", $ array [$ I]):
$ This-> NEW_WHOIS = trim (substr (trim ($ array [$ I]), (strlen (trim ($ array [$ I])-13) * (-1 )));
$ This-> FURTHER_INFO = 1;
Endif;
$ I ++;
Endwhile;

Fclose ($ ptr );

If ($ this-> DATA_COUNT> $ this-> DATA_MIN ):
Return ($ array );
Else:
$ This-> ERROR ++;
Endif;
Else:
$ This-> ERROR ++;
Endif;
}


// Query domestic domain names
Function cnrawlookup ($ query, $ server ){
If (! $ Query ):
Return ("");
Endif;

$ Ptr = $ this-> connect ($ server );
If ($ ptr ):
If (! Ereg ($ query, "n $ ")):
$ Query. = "n ";
Endif;
Fputs ($ ptr, "$ query ");
$ I = 0;
$ This-> FOUND = 1;
While (! Feof ($ ptr )):
$ Array [$ I] = fgets ($ ptr, $ this-> MAXLEN );
$ This-> DATA_COUNT + = strlen (chop ($ array [$ I]);
If (eregi ("No match for", $ array [$ I]) | eregi ("No entries found", $ array [$ I]):
$ This-> FOUND = 0;
Elseif (eregi ("WHOIS database is down", $ array [$ I]):
$ This-> ERROR ++;
$ This-> FOUND = 0;
Elseif (eregi ("Please wait a while and try again", $ array [$ I]):
$ This-> ERROR ++;
$ This-> FOUND = 0;
Break;
Endif;
$ I ++;
Endwhile;
Fclose ($ ptr );

If ($ this-> DATA_COUNT> $ this-> DATA_MIN ):
Return ($ array );
Else:
$ This-> ERROR ++;
Endif;
Else:
$ This-> ERROR ++;
Endif;
}
};



$ MyWHOIS = new whois ();

$ Thisname = $ servername. $ domainname;
// Select whois server based on the domestic domain name or international domain name
If (ereg (". cn $", $ thisname ))
{
$ MyWHOIS-> WHOIS_SERVER = "whois.cnnic.net.cn ";
$ Array = $ myWHOIS-> cnrawlookup ($ thisname, $ myWHOIS-> WHOIS_SERVER );
}
Else
{
$ MyWHOIS-> WHOIS_SERVER = "whois.internic.net ";
// $ MyWHOIS-> WHOIS_SERVER = "whois.networksolutions.com ";
$ Array = $ myWHOIS-> rawlookup ($ thisname, $ myWHOIS-> WHOIS_SERVER );
}



Echo"
". $ Thisname ."
";
Echo "";
$ X = 0;
While ($ x {
Echo "$ x ";
Echo "$ array [$ x]";
$ X ++;
}
Echo"
";

If (! Ereg (". cn $", $ thisname ))
{
Echo"
Furth infomation
";
$ Array_further = $ myWHOIS-> rawlookup ($ thisname, $ myWHOIS-> NEW_WHOIS );

Echo "";
$ X = 0;
While ($ x {
Echo "$ x ";
Echo "$ array_further [$ x]";
$ X ++;
}
Echo"
";
}

?>

The above introduces the whais class in php (modified version), including the whais class content, hope to be helpful to friends interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.