月度归档:2020年03月

[性能测试]免费IP归属地查询接口汇总

目前做一个项目,需要判断是国内还是国外IP,具体要求为接口稳定,速度快,免费,不异常。所以我整理了优质的接口供大家筛选。

一、淘宝API接口

http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]

ip参数中添加你要查询的ip,返回数据为json格式。包括国家地区运营商等数据。

{"code":0,"data":{"ip":"221.218.209.125","country":"中国","area":"","region":"北京","city":"北京","county":"XX","isp":"联通","country_id":"CN","area_id":"","region_id":"110000","city_id":"110100","county_id":"xx","isp_id":"100026"}}

测评结果:

查询到: 110 未查询到: 0 报错: 890 执行时间 314.29S

二、ip-api接口

可以调用http://ip-api.com/json/接口查询域名,默认为当前请求的IP

# 国际化英文显示
http://ip-api.com/json/
# 修改语言编码
http://ip-api.com/json/?lang=zh-CN
# 修改IP信息
http://ip-api.com/json/115.191.200.34?lang=zh-CN

返回数据为json格式。包括国家地区运营商等数据。

{"as":"AS6106 University of California, Riverside","city":"里弗赛德","country":"美国","countryCode":"US","isp":"University of California, Riverside","lat":33.9473,"lon":-117.4008,"org":"University of California, Riverside","query":"169.235.24.133","region":"CA","regionName":"加利福尼亚州","status":"success","timezone":"America/Los_Angeles","zip":"92521"}

测评结果:

没有测试完,应该是有调用量限制,不过一开始查询比淘宝快。

三、搜狐IP地址查询

http://pv.sohu.com/cityjson
# 设置编码
http://pv.sohu.com/cityjson?ie=utf-8

当前只能查到区域信息,数据划分不是很明确:

var returnCitySN = {"cip": "221.218.209.125", "cid": "110108", "cname": "北京市海淀区"};

四、太平洋IP地址查询

太平洋IP地址库API接口为:

http://whois.pconline.com.cn/ipJson.jsp?ip=xxx.xxx.xxx.xxx&json=true

查询到数据如下:

{"ip":"221.218.209.125","pro":"北京市","proCode":"110000","city":"北京市","cityCode":"110000","region":"海淀区","regionCode":"110108","addr":"北京市海淀区 联通ADSL","regionNames":"","err":""}

太平洋IP查询的更多接入方式查看http://whois.pconline.com.cn/

测试结果:

查询到: 1000 未查询到: 0 报错: 0 执行时间 68.51S

测试结果

国内使用,

  • TOP1 太平洋IP为最优选择,国内准确率高,同时可以判断出国家
  • TOP2 淘宝API可用于国内查询,国外经常报错,速度不是太快
  • TOP3 ip-api接口,精确度高,但是速度不稳定,如果你对查询精度要求高,可以使用这个接口。
  • TOP4 搜狐IP没法测试

测试代码:

import requests
import random
import json
import time

i = 0
has = 0
nohas = 0
error = 0
start = time.time()
while i < 1000:
    i += 1
    ip = "115."+str(random.randint(1,199))+"."+str(random.randint(1,199))+"."+str(random.randint(1,199))
    try:
        response = requests.get(url="http://ip.taobao.com/service/getIpInfo.php",params={"ip":ip})
        texts = response.text
        infos = json.loads(texts)
        print(infos)
        country = infos["data"]["country"]
        if country:
            has += 1
        else:
            nohas += 1
    except:
        error += 1
end = time.time()
execution_time = end-start
print("查询到:",has,"未查询到:",nohas,"报错:",error,"执行时间",execution_time)

国内大型网站提供的 IP API接口调用方法

今天群里有人问到 IP API 的调用方法,他说一些实用的jQuery代码片段中的 jQuery得到用户IP 不起作用。我查看了下,调用没有问题,于是想到应该是 GWF 干的好事。如我上网经常在墙外,调用结果如下:

关于这个不多说了,越说越气愤。我们看看如何使用国内几个门户网站提供的 IP API 的使用方法,我个人不喜欢腾讯提供的,它使用的是gb2312编码,这里仅以搜狐提供的 IP API 说明下。使用时,可先将地址拷贝到浏览器中看下返回结果,在进行合理的调用。如调用搜狐提供的 IP API 方法如下:


结果如下:

群里朋友使用时不注意返回类型,从而使用getJSON来获取结果,导致失败。以下是几大门户网站提供的 IP API 地址:

腾讯的IP地址API接口地址:http://fw.qq.com/ipaddress
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=219.242.98.111
搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip
淘宝的IP接口地址: http://ip.taobao.com/instructions.php 
还有一个 API比较全面 
http://whois.pconline.com.cn 这个很强大 也比较详细 
但是这个有问题 他JSON格式 属于回调 

在自定义表单中获取提交者的IP地址信息

有时候我们需要在自定义表单中获取访问者的ip地址信息,那么要怎么实现呢?

1、首先我们在后台自定义表单中建立一个字段为:uip

2、在自定义表单中的写法如下,把uip这个字段作为隐藏字段:

<input type="hidden" name="uip" id="uip" value="">
<script type="text/javascript" src="http://pv.sohu.com/cityjson/getip.aspx" charset="utf-8"></script>
<script type="text/javascript">
var ip=returnCitySN.cip+"-"+returnCitySN.cname;
document.getElementById("uip").value=ip;
</script>

齐全的IP地址查询接口及调用方法(转)

  • 设计蜂巢IP地址查询接口:http://www.hujuntao.com/api/ip/ip.php
  • 腾讯IP地址查询接口:http://fw.qq.com/ipaddress
  • 新浪IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
  • 搜狐IP地址查询接口:http://pv.sohu.com/cityjson
  • 谷歌IP地址查询接口:http://j.maxmind.com/app/geoip.js
  • 有道IP地址查询接口:http://www.youdao.com/smartresult-xml/search.s
  • 1616 IP地址查询接口:http://w.1616.net/chaxun/iptolocal.php
  • 126 http://ip.ws.126.net/ipquery
  • hao123 http://app.hao123.com/ipquery/getcity.php?rtype=2

设计蜂巢接口调用方法

API请求地址 : http://www.hujuntao.com/api/ip/ip.php?参数

参数说明:

  • IP : IP地址 可选
  • format : 输出格式 json 可选 默认为JS

JSON调用示例:
请求地址:http://www.hujuntao.com/api/ip/ip.php?callback=jQuery17107324279078748077_1327024991339&format=json&_=1327024991363

{
“ip”: “117.89.35.58″,
“province”: “\u6c5f\u82cf”,
“city”: “\u5357\u4eac”
}

腾讯接口调用方法

  1. <script src=”http://fw.qq.com/ipaddress”></script>
  2. <script type=”text/javascript”>
  3. //反回格式如下:
  4. //var IPData = new Array(“117.89.35.98″,””,”江苏省”,”南京市”);
  5. console.log(IPData[0]+’,’+IPData[2]+’,’+IPData[3]) //117.89.35.98,江苏省,南京市
  6. </script>

该方法已经不可用,访问http://fw.qq.com/ipaddre显示“The page you are looking for is temporarily unavailable.
Please try again later.”。腾讯对Referer做了判断,虽然可以通过PHP伪造Referer,但是实际测试中获得的是服务器的IP地址而不是客户端的IP地址。我们一起鄙视一下腾讯。

新浪接口调用方法

  1. <script src=”http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js”></script>
  2. <script type=”text/javascript”>
  3. //format参数设置返回格式 js/json
  4. //ip 要查询IP 可选
  5. //反回格式如下:
  6. //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”:””};
  7. console.log(remote_ip_info[“country”]+’,’+remote_ip_info[“province”]+”省”+’,’+remote_ip_info[“city”]+”市”)//中国,江苏省,南京市
  8. </script>
 

搜狐接口调用方法

  1. <script src=”http://pv.sohu.com/cityjson?ie=utf-8″></script>
  2. <script type=”text/javascript”>
  3. //默认为GBK可通过ie参数设置编码
  4. console.log(returnCitySN[“cip”]+’,’+returnCitySN[“cname”])//117.89.35.98,江苏省南京市
  5. </script>
 

谷歌接口调用方法

据说是谷歌的,不知道是不是真的。

  1. <script src=”http://j.maxmind.com/app/geoip.js”></script>
  2. <script type=”text/javascript”>
  3. console.log(geoip_country_name()+’,’+geoip_region_name()+’,’+geoip_city())//China,Jiangsu,Nanjing
  4. </script>
 

有道接口调用方法

调用接口示例:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=123.233.157.9

返回XML数据

<?xml version=”1.0″ encoding=”gbk”?>
<smartresult>
<product type=”ip”>
<ip>123.233.157.9</ip>
<location>山东省济南市 网通</location>
</product>
</smartresult>

返回JSON数据

fYodaoCallBack(1, {‘product’:’ip’,’ip’:’192.168.1.1′,’location’:’局域网 对方和您在同一内部网’} , ”);