在日常开发过程中,经常有通过IP去获取用户位置,或在服务器日志中查看到各种各样的ip地址,如何通过ip地址去获取用户的信息呢?比如所在城市,网络提供商是联通,移动,电信呢,通过接口我们可以实现这些功能。
一、前言
使用聚合数据的ip接口实现功能,thinkphp框架在3.2版本中有ip定位功能,至于3.2之前的版本就不清楚了,没用过,哈哈~~~~,为啥使用聚合数据的呢,有两个点吧,一接口完全免费,二接口代码明了简洁。接口需要appkey,到聚合官网自行申请。
二、接口示例
演示地址:https://www.shenlin.ink/open/tool/index.html
三、代码
1、控制器代码
<?php
/**
* Created by PhpStorm.
* User: 申霖
* Date: 2019/7/25 0025
* Time: 下午 6:18
*/
namespace app\api\controller;
use think\Controller;
class Tool extends Controller
{
/**
* view
* @return \think\response\View
* @author 申霖
* @time 2019/7/25 0025 下午 7:08
*/
public function index()
{
return view('tool/index');
}
/**
* ip地址查询
* @return \think\response\Json|\think\response\View
* @author 申霖
* @time 2019/7/25 0025 下午 6:37
*/
public function ip()
{
$ip = input('post.ip');
if($ip) {
header('Content-type:text/html;charset=utf-8');
//配置您申请的appkey
$appkey = "ef25bc7b023de945d316b6a5*****";
//************1.根据IP/域名查询地址************
$url = "http://apis.juhe.cn/ip/ip2addr";
$params = [
"ip" => $ip,//需要查询的IP地址或域名
"key" => $appkey,//应用APPKEY(应用详细页查询)
"dtype" => "json",//返回数据的格式,xml或json,默认json
];
$paramString = http_build_query($params);
$content = $this->juheCurl($url, $paramString);
$result = json_decode($content, true);
if($result) {
if($result['error_code'] == '0') {
return json([
'code' => 200,
'msg' => 'success',
'data' => $result['result']
]);
} else {
return json([
'code' => $result['error_code'],
'msg' => $result['reason'],
'data' => ''
]);
}
} else {
return json([ 'code' => 101, 'msg' => '请求失败', 'data' => '' ]);
}
} else {
return json(['code'=>100,'msg'=>'请输入IP']);
}
}
/**
* 请求接口返回内容
* @param $url
* @param bool $params
* @param int $ispost
* @return bool|mixed
*/
public function juheCurl($url, $params = false, $isPost = 0)
{
$httpInfo = [];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($isPost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
}
2、视图代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>IP地址查询</title>
<meta name="keywords" content="IP,地址,查询">
<meta name="description" content="IP地址查询">
<link rel="icon" href="https://www.shenlin.ink/favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" href="/blog/css/layui.css">
<style>
.layui-form-item .layui-input-inline {
width: 100%;
}
.layui-form {
padding-top: 130px;
width: 60%;
margin: 0 auto;
height: 100px;
}
.layui-btn {
width: 100%;
}
</style>
</head>
<body>
<div class="layui-container">
<form class="layui-form">
<div class="layui-form-item">
<div class="layui-input-inline">
<input placeholder="请填写IP" name="ip" required lay-verify="required"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-inline">
<button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
</div>
</div>
</form>
</div>
<script src="/blog/jquery-2.1.4.min.js"></script>
<script src="/blog/layui.js"></script>
<script>
layui.use(['layer', 'form'], function () {
var form = layui.form, layer = layui.layer;
form.on('submit(*)', function (data) {
$.ajax({
type: 'post',
url: '/open/tool/ip.html',
data: data.field,
async: false,
dataType: "json",
success: function (data) {
if (data.code === 200) {
layer.open({
title: data.msg,
content: "<p>地区:"
+ data.data.area +
"</p><p>服务商:"
+ data.data.location +
"</p>"
})
} else {
layer.msg(data.data);
}
},
error: function () {
layer.msg("接口错误");
}
});
return false;
});
});
</script>
</body>
</html>
Html代码看起来永远是那么乱的~~~~~
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有