• HOME
  • DOCS
  • WTF
  • TECH
  • LIFE
  • PAGES
    • ARCHIVE
    • TAGS
    • ABOUT
    • FRIENDS
    • RSS
  • TOOLS
    • GEO
    • RANDOM()
    • GOO.GL
    • CSS HEART
Aj's Blog

记录时间溜走的瞬间和折腾过的那些事

新浪JSAPI: ip2city实现

2013-12-03  TECH  API  IP  jquery  onload  城市  新浪  

一、接口介绍

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.8

pconline
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 + ' (获取中...)');
};
下一篇:   centos6 ssh “Server refused our key” Error
上一篇:   关于产品经理/需求分析的一点资料
暂无评论

Cancel reply