根据浏览者的IP切换成不同语言,新浪网易IP地区信息查询API接口调用方法

1 篇文章 0 订阅

工作需求,要根据浏览者的IP切换成不同语言的页面,记了下面的代码。

主要是获取IP对应的地区,用的是新浪的IP查询接口。

<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script>
<script type="text/javascript">
//console.log(remote_ip_info["country"]+’,'+remote_ip_info["province"]+"省"+’,'+remote_ip_info["city"]+"市")//中国,江苏省,南京市
//alert('\n国家:'+remote_ip_info.country+'\n省份:'+remote_ip_info.province+'\n城市:'+remote_ip_info.city+'\n区:'+remote_ip_info.district+'\nISP:'+remote_ip_info.isp+'\n类型:'+remote_ip_info.type+'\n其他:'+remote_ip_info.desc + '\nIP段: ' + remote_ip_info.start + '-' + remote_ip_info.end);   

var indexs = 0;
if(remote_ip_info["country"] == "美国") {
    indexs = 1;
}else if(remote_ip_info["country"] == "俄国"){
    indexs = 2;
}

	var sddl = document.getElementById('select');
	sddl.selectedIndex = indexs;
    //alert(indexs);
    changeLan()
</script>


以下是查询的资料,上面不太清楚的可以阅读下面的资料。

====================================


通过IP地址获取对应的地区信息通常有两种方法:

1)自己写程序,解析IP对应的地区信息,需要数据库。

2)根据第三方提供的API查询获取地区信息。

第一种方法,参见文本《通过纯真IP数据库获取IP地址对应的地区信息》,这种方法的维护代价较高,而且对自己的服务器有一定的压力。

随着技术的开放,第三方已经逐渐免费开放相应的API。经过测试,目前网易和新浪提供的较为稳定易用。

1)新浪API接口

新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=IP地址

举例:

测试用例:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=115.156.238.114

返回以下信息:

    var remote_ip_info = {"ret":1,"start":"115.156.128.0","end":"115.156.255.255","country":"\u4e2d\u56fd","province":"\u6e56\u5317","city":"\u6b66\u6c49","district":"","isp":"\u6559\u80b2\u7f51","type":"\u5b66\u6821","desc":"\u534e\u4e2d\u79d1\u6280\u5927\u5b66\u4e1c\u6821\u533a"};   

文字经过utf-8编码,分别对应不同的信息。

可以通过javascript跨域直接调用,如(使用jquery):

    $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='+ip, function(_result){   
    if (remote_ip_info.ret == '1'){   
    alert('IP 详细信息:', 'IP:'+ip+'<BR>国家:'+remote_ip_info.country+'<BR>省份:'+remote_ip_info.province+'<BR>城市:'+remote_ip_info.city+'<BR>区:'+remote_ip_info.district+'<BR>ISP:'+remote_ip_info.isp+'<BR>类型:'+remote_ip_info.type+'<BR>其他:'+remote_ip_info.desc);   
    } else {   
    alert('错误', '没有找到匹配的 IP 地址信息!');   
    }   

2)网易有道API接口

查询IP地址:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=IP地址

举例:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=115.156.238.114

返回xml文本信息:

    <?xml version="1.0" encoding="gbk"?>  
    <smartresult>  
    <product type="ip">  
    <ip>115.156.238.114</ip>  
    <location>湖北省武汉市 教育网</location>  
    </product>  
    </smartresult>   

网上有不少方法, 腾讯的当掉了, 搜狐的又不够准确, 纠结了会觉得还是新浪的靠谱点.

对于小流量的查询直接使用JS输出就OK了.

<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script>
<script type="text/javascript">
//format参数设置返回格式 js/json
//ip 要查询IP 可选
//反回格式如下:
//var remote_ip_info = {"ret":1,"start":"117.89.35.0","end":"117.89.35.255","country":"\u4e2d\u56fd","province":"\u6c5f\u82cf","city":"\u5357\u4eac","district":"\u767d\u4e0b","isp":"\u7535\u4fe1","type":"","desc":""};
console.log(remote_ip_info["country"]+’,'+remote_ip_info["province"]+"省"+’,'+remote_ip_info["city"]+"市")//中国,江苏省,南京市
</script>


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

概念西瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值