新浪,有道,ip138提供了将ip地址转换为真实地址的接口,这个控件的作用就是将这些接口封装起来,实现js将ip地址转换为真实地址。
使用方法很简单,如有html
<div class="query_ip">202.103.22.1</div>
调用如下js
$(".query_ip").getaddress({ type:"youdao" // 支持youdao|sina|ip138 (有道速度快且准确,新浪快,ip138较慢) });
则将HTML修改为
<div class="query_ip">202.103.22.1 湖北省十堰市 东风电信</div>
具体JS源码如下
(function(){ /* * 默认配置 */ var domainAddressConf = { source : { "sina":"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=", "youdao":"http://youliao.sinaapp.com/app/getip.php?callback=?&type=youdao&ip=", "ip138":"http://youliao.sinaapp.com/app/getip.php?callback=?&ip=" }, type : null, charset : "gb2312" } function getAddress( self,conf ){ var type = conf.type || "youdao", ipadress = conf.source[type] + self.text(), api = new getAddressApi( type,ipadress ); api.ready(function( data ){ self.append( data ); }) return self; } function getAddressApi( type,conf ){ var root = this; this.changeData = function( data ){ this.data = data; return this; } return new getAddressApi[type]( root,conf ); } /* * 获取新浪ip数据 */ getAddressApi.sina = function( root,dataUri ){ this.root = root; $.getScript(dataUri,function(){ root.changeData(remote_ip_info); }); } getAddressApi.sina.prototype = { ready: function( fn ){ var self = this, data = self.root.data; setTimeout(function(){ if( data ){ fn.call(document," "+data.province+"省"+data.city+"市 "+data.desc); }else{ self.ready(fn); } },13); } } /* * 获取搜狐ip数据 */ getAddressApi.youdao = function( root,dataUri ){ this.root = root; $.getJSON( dataUri, function( data ){ root.changeData(data); }); } getAddressApi.youdao.prototype = { ready: function( fn ){ var self = this, data = self.root.data; setTimeout(function(){ if( data ){ fn.call(document," "+data[0]); }else{ self.ready(fn); } },13); } } /* * 获取ip138数据 */ getAddressApi.ip138 = function( root,dataUri ){ this.root = root; $.getJSON( dataUri, function( data ){ root.changeData(data[0].replace("查询结果:","")); }); } getAddressApi.ip138.prototype = { ready: function( fn ){ var self = this, data = self.root.data; setTimeout(function(){ if( data ){ fn.call(document," "+data); }else{ self.ready(fn); } },13); } } $.fn.getaddress = function(conf) { // 判断是否已实例化 if (this.data("domainaddress")) { return this; } // 配置 conf = $.extend(true, {}, domainAddressConf, conf); this.each(function() { var el = new getAddress( $(this), conf ); $(this).data("domainaddress", el); }); return conf.api ? el : this; }; })(jQuery);
因为有道和ip138使用的是xml,为解决跨域,增加了一个php做中转,源码如下
@header("Content-Type: text/html; charset=utf-8"); $type = $_GET['type']; if( $type == "youdao" ){ $xml_file_content = fopen_url("http://www.youdao.com/smartresult-xml/search.s?type=ip&q=".$_GET['ip']); $xml = simplexml_load_string( $xml_file_content ); echo $_GET['callback'].'('.json_encode($xml->product->location).')'; }else{ $xml_file_content = fopen_url("http://wap.ip138.com/ip.asp?ip=".$_GET['ip']); preg_match_all( "/\<p \>((.|\n)*)\< \/p\>/s",$xml_file_content,$content ); foreach( $content[1] as $result ) { preg_match_all( "/\<b \>((.|\n)*)\< \/b\>/",$result, $location ); echo $_GET['callback'].'('.json_encode($location[1]).')'; } } //Curl 获取网址内容 function fopen_url($url) { if (function_exists('file_get_contents')) { $file_content = @file_get_contents($url); } elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){ $i = 0; while (!feof($file) && $i++ < 1000) { $file_content .= strtolower(fread($file, 4096)); } fclose($file); } elseif (function_exists('curl_init')) { $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); curl_setopt($curl_handle, CURLOPT_FAILONERROR,1); curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); $file_content = curl_exec($curl_handle); curl_close($curl_handle); } else { $file_content = ''; } return $file_content; }
DEMO:http://www.zhangjingwei.com/getIp_0204_addip138_last.zip
