MENU

修正Whois查询 适配中文域名以及获取输入任意域名的主域名

March 14, 2022 • 已被 522 位童鞋围观过 • 代码分享

使用 idn_to_ascii 函数将中文域名转换为 IDNA ASCII 兼容格式
以 . 为条件切割域名为数组
循环获得第一个.后面的字符串 匹配whois服务器,成功就跳出循环
输出

//获取域名  whois 服务器
function get_whois_server($domain){
    $domain = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
    if(preg_match("/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/",$domain)){
        $re["status"]="error";
        $re["message"]="Use IP Lookup";
        return $re;
    }
    if(!preg_match("/^([-a-zA-Z0-9]{1,100})\.([-a-z0-9\.]{2,})$/i", $domain)){
        $re["status"]="error";
        $re["message"]="Error:Wrong format!";
        return $re;
    }
    global $whoisservers;
    $whoisserver = "";


    $domain_arr=explode(".",$domain);
    $domains=$domain;
    foreach($domain_arr as $k => $v){
        $apex_domains=$domains;
        $dotpos=strpos($domains,".");
        $domains=strtolower(substr($domains,$dotpos+1));
        if(isset($whoisservers[$domains])){
            $whoisserver = $whoisservers[$domains];
            $apex_domain=idn_to_utf8($apex_domains,IDNA_DEFAULT,INTL_IDNA_VARIANT_UTS46);
            $gTLD=$domains;
            break;
        }
    }
    if(!$whoisserver or $whoisserver=="unknown") {
        $re["status"]="error";
        $re["message"]="No appropriate Whois server found for $domain domain!";
        return $re;
    }
    $re["status"]="success";
    $re["domain"]=$apex_domain;
    $re["gTLD"]=$gTLD;
    $re["whois_server"]=$whoisserver;
    return  $re;
}

返回信息

{
  "action": "whois_server",
  "query": "dasd199.小孩采集.中国",
  "status": "success",
  "domain": "小孩采集.中国",
  "gTLD": "xn--fiqs8s",
  "whois_server": "cwhois.cnnic.cn"
}
Last Modified: September 28, 2023