PHP实现域名whois查询,附顶级域名及WHOIS服务器一览

2020-03-27   阅读:2725   类型:后端   分类:PHP    标签: PHP

案例例子:


PHP代码:

<?php
//    域名whois查询
    public function whois(Request $request){
        if($request->isMethod('post')){
            $url =$request->url;
            $request_url=$this->LookupDomain($url);
            $regex = "/Name Server: [A-Z0-9]*\.[A-Z0-9.]+/is";
            preg_match_all($regex,$request_url,$matches,PREG_PATTERN_ORDER);
            $strrpe=str_replace("Name Server: ","",$matches[0]);
            $alll= explode("\r\n", $request_url);

            return response()->json(['code'=>200,'message'=>['data'=>$alll,'dns'=>implode("<br>", $strrpe)]]);


            if($arr['code']==200){
                return response()->json(['code'=>200,'message'=>$arr]);
            }else{
                return response()->json(['code'=>500,'message'=>$arr['msg']]);
            }

        }

        return view('tools.icp.whois');
    }
    public function LookupDomain($domain){
        $whoisservers = array(
            "aero"=>"whois.aero",
            "arpa" =>"whois.iana.org",
            "asia" =>"whois.nic.asia",
            "at" =>"whois.nic.at",
            "be" =>"whois.dns.be",
            "biz" =>"whois.biz",
            "br" =>"whois.registro.br",
            "ca" =>"whois.cira.ca",
            "cc" =>"whois.nic.cc",
            "cn" =>"whois.cnnic.net.cn",
            "com" =>"whois.verisign-grs.com",
            "gov" =>"whois.nic.gov",
            "in" =>"whois.inregistry.net",
            "co.in" =>"whois.inregistry.net",
            "net.in" =>"whois.inregistry.net",
            "org.in" =>"whois.inregistry.net",
            "ind.in" =>"whois.inregistry.net",
            "firm.in" =>"whois.inregistry.net",
            "info" =>"whois.afilias.info",
            "int" =>"whois.iana.org",
            "is" =>"whois.isnic.is",
            "it" =>"whois.nic.it",
            "jobs" =>"jobswhois.verisign-grs.com",
            "me" =>"whois.meregistry.net",
            "mil" =>"whois.nic.mil",
            "mobi" =>"whois.dotmobiregistry.net",
            "museum" =>"whois.museum",
            "name" =>"whois.nic.name",
            "net" =>"whois.verisign-grs.net",
            "org" =>"whois.pir.org",
            "pro" =>"whois.registrypro.pro",
            "tc" =>"whois.adamsnames.tc",
            "tel" =>"whois.nic.tel",
            "travel" =>"whois.nic.travel",
            "tv" => "whois.www.tv",
            "co.uk" =>"whois.nic.uk",
            "org.uk" =>"whois.nic.uk",
            "us" =>"whois.nic.us",
            "ws" =>"whois.website.ws",
            "top" =>"whois.nic.top",
            "xin" =>"whois.nic.xin"
        );


        $dotpos=strpos($domain,".");
        $domtld=substr($domain,$dotpos+1);

        $whoisserver = $whoisservers[$domtld];

        if(!$whoisserver) {
            return "Error: No appropriate Whois server found for <b>$domain</b> domain!";
        }
        $result = $this->QueryWhoisServer($whoisserver, $domain);
        if(!$result) {
            return "Error: No results retrieved $domain !";
        }

        return  $result;
    }

     public function QueryWhoisServer($whoisserver, $domain) {
        $port = 43;
        $timeout = 10;
        $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);
        fputs($fp, $domain . "\r\n");
        $out = "";
        while(!feof($fp)){
            $out .= fgets($fp);
        }
        fclose($fp);
        return $out;

    }
?>

前端代码:

<div class="col-md-9">
    <div class="alert alert-success" role="alert">whois查询工具:通过域名可直接查询该域名whois信息:</div>
    <div class="form-group">
        <input type="text" id="domainurl" class="form-control" >
    </div>
    <button type="button" class="btn btn-info" onclick="cliclk_help()">查询whois备案</button>
    <div class="panel panel-warning" style="margin-top: 15px;">
        <div class="panel-heading">
            <h3 class="panel-title">whois备案查询结果 ,域名 <span class="domainss"></span> 的信息</h3>
        </div>
        <style>
          .listht h5{ padding-top: 40px;}
        </style>
        <div class="panel-body listht">

        </div>
    </div>
</div>
 <script>
	function cliclk_help() {
		var code =$('#domainurl').val();
		if(code==''){
			layer.msg('请先输入域名')
		}
		$.ajax({
			url:"{{route('tools.icp.whois')}}",
			type:'post',
			data:{"url":code},
			datatype:'json',
			success:function(msg){
				console.log(msg)
				if(msg.code==200){
					var data= msg.message.data;

						$('.domainss').text(data[0].split(':')[1])
					var html="";

					html+='<table class="table table-hover table-bordered">\n' +
						'<thead>\n' +
						'<tr>\n' +
						' <th>域名</th>\n' +
						' <th>'+data[0].split(':')[1]+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>注册商</th>\n' +
						' <th>'+data[7].split(':')[1]+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>联系邮箱</th>\n' +
						' <th>'+data[9].split(':')[1]+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>联系电话</th>\n' +
						' <th>'+data[10].split(':')[1]+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>创建时间</th>\n' +
						' <th>'+data[5].split(':')[1].substring(0, data[5].split(':')[1].length - 3)+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>过期时间</th>\n' +
						' <th>'+data[6].split(':')[1].substring(0, data[6].split(':')[1].length - 3)+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>更新时间</th>\n' +
						' <th>'+data[4].split(':')[1].substring(0, data[4].split(':')[1].length - 3)+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>域名服务器</th>\n' +
						' <th>'+data[2].split(':')[1]+'</th>\n' +
						' </tr>\n' +
						'<tr>\n' +
						' <th>DNS</th>\n' +
						' <th>'+msg.message.dns+'</th>\n' +
						' </tr>\n' +
						'</thead>\n' +
						'</table>';

					for (var p in data){
						html+='<p>'+data[p]+'</p>'


					}
					$('.listht').html(html)

				}else{
					$('.listht').html('<h5>'+msg.message+'</h5>')
				}



			},error:function(){
				alert('服务器故障');
			}
		});
	}

</script>


在线域名WHOIS查询(PHP版):

clone git@github.com:jyblogs/WHOIS.git


【附录A】目前的顶级域名及WHOIS服务器一览:


20160904163624290.gif

腾讯云11.11上云拼团Go,2核2G3M云服务器 28元/年

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/297.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×