본문 바로가기
IT 인터넷

[PHP]도메인 남은 기간을 알려주는 PHP 프로그램 (CLASS)

by zzom~ 2024. 8. 23.
반응형

웹에서 사용방법 : http://zzom.co.kr/whois.php?domain=zzom.co.kr
쉘에서 사용방법 : #>php -q whois.php zzom.co.kr

도메인 구매 홈페이지 가면 다 검색 할 수 있지만 자체적으로 활용이 필요하면 사용해볼만한 소스.
 
클래스 형식으로 만들었습니다.

if (!$domain)
{
   $domain = $argv[1];
}
$GetExpire = new GetExpireDate;
$GetExpire->GetInfo($domain);


이렇게 해주시면 사용 가능 합니다. 
필요하신분, 공부하면서 활용해 볼 수 있을 것 같네요.

<?
if (!$domain)
{
   $domain = $argv[1];
}
$GetExpire = new GetExpireDate;
$GetExpire->GetInfo($domain);

class GetExpireDate
{
var $askNameServerURL = "";
var $ExpireDate = ""; // Set Days
var $Today = "";
var $AfterExpireDays = "Don't";

   function GetExpireDate()
   {
       $this->today = $this->getDate();
   }

   function GetInfo($domain)
   {
       $expDate = $this->GetHtmlRtnDate($domain);        
       if ( $expDate == "none" ) {
           echo "$domain Domain is not exist\n";
           exit;
       }

       //Test//$DiffDay = $this->DateDiff( $expDate, $this->getDate());

       $this->AfterExpireDays = $this->DateDiff( $this->getDate() , $expDate);

       if ($this->AfterExpireDays < 0 ) { // 만료
           echo "$domain is Before $this->AfterExpireDays days was expired \n";
       } else {
           echo "$domain is After $this->AfterExpireDays days will expired \n";
       }

   }

   function getDate()
   {
       $date = getdate();
       return sprintf("%04d-%02d-%02d",$date[year],$date[mon],$date[mday]);
   }

   function GetHtmlRtnDate($domain)
   {

       $buffer = "";
       $stDate = "none";
       switch ( $this->GetURL($domain) )
       {
           case "kr": // Korea Domain

               $fp = fopen($this->askNameServerURL,"r");        
               while($line = fgets($fp,1000)){
                   if (!is_bool(strpos($line,"Expiration Date"))){
                       $stDate =  $this->GetDateFromExpDate($line);
                       fclose($fp);
                       break;
                   }
               }
               break;
           case "com":
           case "net":
           case "org":
               $line = shell_exec($this->askNameServerURL ." | grep  \"Expiration\"");
               if ($line == "") break;
               $stDate =  $this->GetDateFromExpDate($line);
               $stDate = date("Y-m-d", strtotime($stDate)) ;
               break;
           default : // non Korea Domain com.net.org etc...
               exit;
               break;
       }
       return $stDate;

   }
   function GetDateFromExpDate($string)
   {
       preg_match("/[^\:\/]+$/",$string,$arr);
       $arr[0] = str_replace(".","-",$arr[0]); // korea domain using date seperater dot(.)
       $arr[0] = str_replace(" ","",$arr[0]);
       return $arr[0];
   }
   function GetURL($domain)
   {
       preg_match("/[^\.\/]+$/",$domain, $arr);
       switch($arr[0])
       {
           case "kr":
               $this->askNameServerURL = "http://whois.nic.or.kr/whois/webapisvc?VALUE=$domain";
               break;
           case "com":
           case "net":
           case "org":
               $this->askNameServerURL = "whois $domain | grep  \"Expiration Date\"";
               break;
           default:
               echo "Don't Supporting whois\n";
               exit;
               break;
       }
       return $arr[0];
   }

   function DateDiff($todate, $expdate)
   {

       $time =  (strtotime($expdate) - strtotime($todate))  / 86400;
       return $time;
   }
}
?>
반응형