因为项目需要查询多个域名的到期时间和注册时间,这里用到过,所以总结下通过这个项目学到的东西吧。
方法1,phpwhois
互联网的好处就是开源共享,这里百度PHP查询域名whois信息有大神封装好的信息。
官网:http://www.phpwhois.org/
图片:
总结:优点是拿过来就可以用,缺点是文件比较多,需要自己处理
方法2,函数处理
来源:http://www.oschina.net/code/snippet_202258_8574
处理后的代码:
<?php function whois_query($domain) { $domain = parse_url(strtolower(trim($domain))); // 屏蔽本地报错 @$domain = $domain['host'] ? $domain['host'] : $domain['path']; $domain = preg_replace('/^www\./i', '', $domain); $_domain = explode('.', $domain); $lst = count($_domain) - 1; $ext = $_domain[$lst]; $servers = array( "biz" => "whois.neulevel.biz", "com" => "whois.internic.net", "us" => "whois.nic.us", "coop" => "whois.nic.coop", "info" => "whois.nic.info", "name" => "whois.nic.name", "net" => "whois.internic.net", "gov" => "whois.nic.gov", "edu" => "whois.internic.net", "mil" => "rs.internic.net", "int" => "whois.iana.org", "ac" => "whois.nic.ac", "ae" => "whois.uaenic.ae", "at" => "whois.ripe.net", "au" => "whois.aunic.net", "be" => "whois.dns.be", "bg" => "whois.ripe.net", "br" => "whois.registro.br", "bz" => "whois.belizenic.bz", "ca" => "whois.cira.ca", "cc" => "whois.nic.cc", "ch" => "whois.nic.ch", "cl" => "whois.nic.cl", "cn" => "whois.cnnic.net.cn", "cz" => "whois.nic.cz", "de" => "whois.nic.de", "fr" => "whois.nic.fr", "hu" => "whois.nic.hu", "ie" => "whois.domainregistry.ie", "il" => "whois.isoc.org.il", "in" => "whois.ncst.ernet.in", "ir" => "whois.nic.ir", "mc" => "whois.ripe.net", "to" => "whois.tonic.to", "tv" => "whois.tv", "ru" => "whois.ripn.net", "org" => "whois.pir.org", "aero" => "whois.information.aero", "nl" => "whois.domain-registry.nl" ); if (!isset($servers[$ext])) { die('错误: 未找到为对的whois提供商!'); } $nic_server = $servers[$ext]; $output = ''; // 请求数据: 服务器 端口 if ($conn = fsockopen($nic_server, 43)) { fwrite($conn, $domain . "\r\n"); while (!feof($conn)) { $output .= fgets($conn); } fclose($conn); } else { die('错误: 不能连接到 ' . $nic_server . '的域名提供商!'); } $tmp = array_filter(explode('<br />',nl2br($output))); foreach ($tmp as $k2 => $v2){ if( strpos($v2,'Creation Date') || strpos($v2,'Registration Date') ){ // net com co cc $regtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Date:')+5))); }elseif ( strpos($v2,'Registration Time') ){ // cn com.cn $regtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Time:')+5))); } if( strpos($v2,'Expiration Date') ){ // net com co $endtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Date:')+5))); }elseif ( strpos($v2,'Expiration Time') ){ // cn $endtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Time:')+5))); } } $savedata['regtime'] = $regtime; $savedata['endtime'] = $endtime; return $data; } $domain = $_GET["domain"] ? $_GET["domain"] : "www.baidu.com"; $result = whois_query($domain); var_dump($result); ?>
总结:用起来的感觉还行,就是不同的域名请求不同地址,经常超时,不过用分页处理的话就会好点,未采纳
方法3,用别人的接口
接口地址:http://whois.263.tw/weixinindex.php?domain=loveteemo.com
(接口有每秒多少次的限制,用下面的方法)
接口地址:http://whois.263.tw/loveteemo.com
这里就不要问我为什么不用阿里云和美橙什么的了,他们有自己对应的加密方式,懒得去研究了,
代码:
public function testAction() { $where = 1; // 获取数据集 $Model = M('xx); $count = $Model->where($where)->count(); $Page = new \Think\AdminPage($count,10); $list = $Model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select(); foreach ($list as $k => $v){ // $WhoisAPI = 'http://whois.263.tw/loveteemo.com';//定义whois api的url // $WhoisAPI = 'http://whois.263.tw/weixinindex.php?domain=loveteemo.com'; //定义whois api的url 有请求限制 $WhoisAPI = 'http://whois.263.tw/'.$v['domainname'];//定义whois api的url $WhoisData = file_get_contents($WhoisAPI);//定义值为读取到的whois信息 //抓取主要内容 $htmlstr = substr( strstr($WhoisData,$v['domainname'].' 以下whois信息来自') , 0 , strpos( strstr($WhoisData,$v['domainname'].' 以下whois信息来自') ,'</pre>') ) ; if($htmlstr){ $htmldata = explode("<br />",nl2br($htmlstr)); foreach ($htmldata as $k2 => $v2){ if( strpos($v2,'Creation Date') || strpos($v2,'Registration Date') ){ // net com co cc $regtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Date:')+5))); }elseif ( strpos($v2,'Registration Time') ){ // cn com.cn $regtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Time:')+5))); } if( strpos($v2,'Expiration Date') ){ // net com co $endtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Date:')+5))); }elseif ( strpos($v2,'Expiration Time') ){ // cn $endtime = date('Y-m-d',strtotime(substr ($v2,stripos($v2,'Time:')+5))); } } $savedata['regtime'] = $regtime; $savedata['endtime'] = $endtime; $savedata['id'] = $v['id']; if(!empty($savedata['regtime'])){ $Model->save($savedata); $html = '添加域名:'.$v["domainname"].'的注册时间:'.$savedata['regtime'].',到期时间:'.$savedata['endtime'].'<br/>'; echo $html; }else{ echo '查询不到域名:'.$v["domainname"].'的信息<br/>'; } }else{ echo '查询不到域名:'.$v["domainname"].'的信息<br/>'; } } }
总结:效率比上面的稍微高点,不用远程请求那些被墙了的whois提供商了,不过处理的时候还是比较慢
方法4,利用站长之家的工具
接口地址:http://whois.chinaz.com/reverse?ddlSearchMode=0
文档:POST传参{
ddlSearchMode:0,
host:host1// 这里需要换行
host2
}
代码:
public function test1Action() { // 定义请求地址 $url = 'http://whois.chinaz.com/reverse?ddlSearchMode=0'; // 默认参数 $data['ddlSearchMode'] = 0; //获取数据 $where = 1; $Model = M('xxx'); $count = $Model->where($where)->count(); $Page = new \Think\AdminPage($count,50); $list = $Model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select(); //根据每页参数拼装POST参数 foreach ( $list as $key1 => $value1){ $data['host'] .= ' '.$value1['domainname']; } //请求 $result = http_post($url,$data); //抓取主要内容 $htmlstr = substr( strstr($result,'<div id="ajaxInfo">') , 0 , strpos( strstr($result,'<div id="ajaxInfo">') ,'<div class="ToolPage clearfix">') ) ; //把原本li的数据分隔成数组 $list1 = explode('</li>',$htmlstr); foreach ($list1 as $key2 => $value2 ){ //数组第一个数描述 最后一个为空 if( $key2 != 0 && $key2 != (count( $list1 )-1) ) { // 数据下的内容根据div分隔成数组 $list2 = explode('</div>', $value2); foreach ($list2 as $key3 => &$value3) { // 数据下的HTML代码清空 $value3 = preg_replace("/<(.*?)>/", "", $value3); // 存入需要的数据 $save[$key2]['domainname'] = $list2[0]; $save[$key2]['regtime'] = $list2[3]; $save[$key2]['endtime'] = $list2[4]; } } } //循环处理数据 foreach ( $save as $key4 => $value4 ){ $res = $Model->where(array('domainname'=>$value4['domainname']))->save($value4); if( $res !== false ){ $html = '添加域名:'.$value4["domainname"].'的注册时间:'.$value4['regtime'].',到期时间:'.$value4['endtime'].'<br/>'; echo $html; }else{ $html = '添加域名:'.$value4["domainname"].'的时候错误!'; echo $html; die; } } }
补上HTTP请求代码
function http_post($url, $param=array()){ $httph =curl_init($url); curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($httph,CURLOPT_RETURNTRANSFER,1); curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); curl_setopt($httph, CURLOPT_POST, 1);//设置为POST方式 curl_setopt($httph, CURLOPT_POSTFIELDS, $param); curl_setopt($httph, CURLOPT_HEADER,1); $rst=curl_exec($httph); curl_close($httph); return $rst; }
这里的信息是批量查询出来的,加上方法3的查询基本可以满足需求了,但是查询的时候总是超时,这里的话因为前面有分页,而没用到,这里在后面补上一段代码:
这里建议当这一页代码执行有存入数据库的操作的时候给予一个标识,等执行完成之后刷新当前页而不是前进一页,这样就不会跳过因为修之后分页查询的时候跳过的数据了。
echo '<meta http-equiv="refresh" content="5;URL=/index.php/Admint/Public/test1?p='. ($p+1) .'">';
上一篇: CentOS下配置SVN环境...
下一篇: 给博客添加CSS3的推荐...