0%

获取设备的外部IP地址 API查询接口

API 查询接口

淘宝

1
2
http://ip.taobao.com/service/getIpInfo.php?ip=myip # 查询当前设备 ip
http://ip.taobao.com/service/getIpInfo.php?ip=39.156.69.79 # 查询指定ip地址

返回数据格式(json 格式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
code: 0,
data: {
ip: "39.156.69.79",
country: "中国",
area: "",
region: "北京",
city: "北京",
county: "XX",
isp: "移动",
country_id: "CN",
area_id: "",
region_id: "110000",
city_id: "110100",
county_id: "xx",
isp_id: "100025"
}
}

优点:国内响应速度快。

缺点:有访问频率限制,小于1qps;无法查询 ipv6 地址。

ip-api

1
2
3
4
5
6
http://ip-api.com/json/ # 查询当前ip
http://ip-api.com/json/39.156.69.79 # 查询指定ip地址
http://ip-api.com/json/2001:4860:4860::8888 # 查询指定ipv6地址

http://ip-api.com/json/?lang=zh-CN # 国际化显示
http://ip-api.com/json/39.156.69.79?lang=zh-CN # 国际化显示

返回数据格式(json 格式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
status: "success",
country: "China",
countryCode: "CN",
region: "GD",
regionName: "Guangdong",
city: "Shenzhen",
zip: "",
lat: 22.5431,
lon: 114.058,
timezone: "Asia/Shanghai",
isp: "China Mobile",
org: "China Mobile",
as: "AS9808 China Mobile",
query: "39.156.69.79"
}

优点:可以查询 ipv6 地址。

缺点:国外服务器,响应速度略慢。

iOS 设备获取外部 IP 地址

Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSURL *url = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error && data) {
NSError *jsonError = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
if (dict && [dict isKindOfClass:[NSDictionary class]]) {
// 解析ip地址
NSString *ip = dict[@"data"][@"ip"];
}
}
}
}];
[dataTask resume];

参考链接