前言
喜欢上网冲浪的同学们应该发现了,最近越来越多的平台开始支持 IP 地址显示了,咱也要紧跟时代的步伐,于是有了今天的文章,为网站加入 IP 显示
自建 IP 地址查询接口
10000次
依据腾讯地图服务自建接口
准备
- 前往腾讯地图官网注册账号(推荐使用微信登录)
- 完成基础认证成为个人开发者(个人开发者拥有一天 10000 次调用)
- 可选择是否认证企业(免费额度更大,可购买套餐包)
创建一个应用
- 前往开发控制台创建一个应用信息自己填
- 在应用中添加一个 KEY(注意:这个 KEY 关乎接口调用,请保存好,避免额度被盗用,
类型选择webService
)
- 创建好后可前往文档查看(官方接口为:https://apis.map.qq.com/ws/location/v1/ip)
创建 api
php 案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?php $ip_user=$_GET['ip']; if (empty($ip_user)) {$ip_user = $_SERVER["REMOTE_ADDR"];} $ip_seekurl='https://apis.map.qq.com/ws/location/v1/ip?ip='.$ip_user.'&key='添加的KEY'; $ip_mes = file_get_contents($ip_seekurl,true); $data = json_decode($ip_mes,true); $var_ip = $data['result']['ad_info']; $nation = $var_ip['nation']; $province = $var_ip['province']; $district=$var_ip['district']; $city = $var_ip['city']; $ip = array( 'nation' => $nation, 'province' => $province, 'city'=>$city, 'district' => $district,); $ip_js = 'var ip_mes = '.json_encode($ip,JSON_UNESCAPED_UNICODE).';'; print($ip_js); return $ip_js; ?>
|
因为淘宝 IP 库在 2022 年 3 月 31 日永久停止服务后,站长就开始找寻可替代方案,一开始的计划是自建数据库,但一想到更新问题就头疼的厉害,在网上冲浪了一波认真查询了一番后决定用腾讯地图提供的服务自建接口
接入本站接口
https://api.cnortles.top/ip
介入方法
参数 |
值 |
ip |
查询地址 IP 采用 GET 方式传输,如果缺省,本站会自动获取客户端 IP 查询! |
使用示例(GET)
1
| https://api.cnortles.top/ip?ip=223.5.5.5
|
参数返回说明
本站默认返回 JS 元组(已定义好变量),精度最大可到区,下面是一段示例
1
| var ip_mes={"nation":"中国","province":"陕西省","city":"西安市","district":"莲湖区"};
|
返回参数 |
值(value) |
nation |
IP 所属国家 |
province |
IP 所属省份 |
city |
IP 所属城市 |
district |
IP 所属区域 |
调用接口
1
| <script type="text/javascript" src="//api.cnortles.top/ip/"></script>
|
使用案列
1 2 3 4 5
| <span id='ip'></span> <script type="text/javascript" src="https://api.cnortles.top/ip/"></script> <script> document.getElementById("ip").innerHTML = ip_mes["nation"]+','+ ip_mes["city"]; </script>
|
因为返回数据是 JS 元组所以只需要使用ip_mes["返回参数值"]即可调用
在配合 JS 输出即可
案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <!doctype html> <html> <head> <meta charset="utf-8"> <link rel="shortcut icon" href="/custom/img/favicon.png"> <title>ERROR:对不起,你的内核不支持!</title> <style> .container { width: 60%; margin: 10% auto 0; background-color: #f0f0f0; padding: 2% 5%; border-radius: 25px; opacity: 0.5;
} footer{ text-align: center; } body{ background: url(https://api.cnortles.top/img?type=wallpaper); } ul { padding-left: 20px; }
ul li { line-height: 2.3 }
a { color: #20a53a } </style> <link rel="stylesheet" href="/custom/css/gongji.css"> <script async src="/custom/js/grayscale.js"></script> </head> <body> <div class="container"> <h1>您所使用的浏览器我们无法提供服务!</h1> <h3>以下信息由系统自动生成</h3> <ul> <li>UA: <span id='ua'></span></li> <li>版本号: <span id='version'></span></li> <li>当前IP:<span id='ip'></span></li> </ul> </div> </body> <script type="text/javascript" src="https://api.cnortles.top/ip/"></script> <script> /*document.getElementById("name").innerHTML=navigator.appName; */ document.getElementById("version").innerHTML=navigator.appVersion; document.getElementById("ua").innerHTML=navigator.userAgent; document.getElementById("ip").innerHTML = ip_mes["nation"]+','+ ip_mes["city"]; console.log('browser kernel is not to bd supported!'); console.log('拒绝IE,从你我开始'); /*alert('拒绝IE,从你我做起,来自'+returnCitySN["cname"]+'的朋友');*/ </script> </html>
|
TO DO
- [ ] 增加多种返回值参数
- [ ] 增加境外 IP 查询
- [ ] 增加 token 授权(暂定)
- [ ] 自建 IP 查询库(开发中)