新浪JSAPI: ip2city实现 2013-12-03 @ TECH APIIPjqueryonload城市新浪
一、接口介绍
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=8.8.8.8
已经忘记最初在哪里看到的,接口用了2年多,一直很稳定。
下面是网络上收集的一些可用的API
新浪
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=txt
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42搜狐
http://pv.sohu.com/cityjson (GBK)
http://pv.sohu.com/cityjson?ie=utf-8 (UTF-8)
http://txt.go.sohu.com/ip/soip淘宝
http://ip.taobao.com/service/getIpInfo.php?ip=8.8.8.8pconline
http://whois.pconline.com.cn/?ip=8.8.8.8
http://whois.pconline.com.cn/ipJson.jsp
http://whois.pconline.com.cn/jsFunction.jsp
二、demo实现
原理很简单,js加载完结果写入objID对应的元素然后删除自己,可重复调用不冲突。
//需要jQuery支持 function getcity(ipStr, objID) { if (ipStr.indexOf("(") > -1) ipStr = ipStr.split("(")[0]; var head = document.getElementsByTagName('head')[0]; var js = document.createElement('script'); js.id = 'sinaip'; js.src = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' + ipStr; head.appendChild(js); js.onload = function() { if (remote_ip_info.province) { $('#' + objID).html(ipStr + ' (' + remote_ip_info.province + '省' + remote_ip_info.city + '市)'); } else { $('#' + objID).html(ipStr + ' (未知)'); } delete remote_ip_info; head.removeChild(js); }; $('#' + objID).html(ipStr + ' (获取中...)'); };
暂无评论