使用sina服务获取客户端IP地址以及所在城市[JS方式]

    新浪提供了许多接口来查询IP所在的城市,不过目前有的已经停止服务了,这里主要介绍一个接口以及用法。

    1.    在HTML页面中引入JS文件

<script type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8"></script>

    该JS文件需要你的电脑能够联网。该接口返回了当前IP的相关信息,返回数据如下:

var returnCitySN = {"cip": "121.33.215.154", "cid": "440100", "cname": "广东省广州市"}

    2.    使用数据:

<script type="text/javascript">
alert(returnCitySN['cip'] + "|" + returnCitySN['cname'])
</script>

    挺简单的。

附上一些其他地址,可以自己测试:

新浪的IP地址查询接口: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

搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson

搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8

搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip

腾讯的接口:http://fw.qq.com/ipaddress (腾讯的接口原本是最好用的,可现在打不开啦)

猜你喜欢

转载自blog.csdn.net/qq_34464926/article/details/80815792