本文共 152 个字数,平均阅读时长 ≈ 1分钟
一、注册腾讯地图KEY
1、注册账号
https://lbs.qq.com/
2、创建应用
打开控制台,左侧有一个应用管理,点击后可创建应用
3、添加Key
选择 WebService API 服务,选择后有三种方案,域名白名单、授权 IP、签名校验,一般我们会部署在服务器上,所以可以选择授权 IP,方便一些
二、修改代码
1、修改文件function.php
路径 Joe/core/function.php
在末尾添加以下代码
/* 腾讯Lbs IP定位服务 */
function curl_tencentlbs_ip($ip) {
//应用Key也就是上面所说的AK,修改成自己的Key即可
$key = 'XXX-XXX';
$url = 'https://apis.map.qq.com/ws/location/v1/ip?ip='.$ip.'&key='.$key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36');
$content = curl_exec($ch);
curl_close($ch);
if($content) {
$json = json_decode($content,true);
if($json['status'] == 0 ) {
$resjson = $json['result']['ad_info'];
if($resjson['province']=='北京市'||$resjson['province']=='天津市'||$resjson['province']=='上海市'||$resjson['province']=='重庆市') {
return $resjson['nation'].$resjson['city'];
}
return $resjson['nation'].$resjson['province'];
}
}
return '';
}
2、修改文件comment.php
路径 Joe/public/comment.php
代码
代码语言:javascript复制<?php echo curl_tencentlbs_ip($comments->ip); ?>
截图