使用PHP根据用户的IP地址获取地址信息:
'bd_ak' => 'yr0Rm***************24HruOyGE', //百度地图的ak
function get_client_city(){
$ip = get_client_ip(); //调用上面的函数进行获取IP地址
$ak = Config('app.bd_ak'); ///申请的ak
$url = file_get_contents("http://api.map.baidu.com/location/ip?ip=$ip&ak=$ak"); //调用百度地图开放接口
$res = json_decode($url,true);
//数据处理
$data['login_ip'] = $ip; //ip
$data['login_xy'] = $res['content']['point']['x'].','.$res['content']['point']['y'];//坐标
$data['login_province'] = $res['content']['address_detail']['province']; //省
$data['login_city'] = $res['content']['address_detail']['city']; //市
$data['login_district'] = $res['content']['address_detail']['district'];//区
$data['login_street'] = $res['content']['address_detail']['street']; //街
$data['login_street_number'] = $res['content']['address_detail']['street_number']; //街道编号
$data['login_city_code'] = $res['content']['address_detail']['city_code']; //城市编号
$data['login_create_time'] = time();
//入库
Db::name('login_log')->insert($data);
}
//表
CREATE TABLE `login_log` (
`login_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户登录记录主键',
`login_user_number` bigint(20) NOT NULL COMMENT '用户编号',
`login_ip` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '用户登录的ip',
`login_xy` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '登录的坐标点',
`login_province` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '登录省',
`login_city` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '登录城市',
`login_district` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '区',
`login_street` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '街',
`login_street_number` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '街道编号',
`login_city_code` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '城市编号',
`login_type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '用户登录类型 0平台用户,1前台用户',
`login_create_time` bigint(20) NOT NULL COMMENT '创建时间',
PRIMARY KEY (`login_id`,`login_user_number`),
KEY `login_create_time` (`login_create_time`) COMMENT '时间索引 搜索',
KEY `login_type` (`login_type`) COMMENT '类型'
) ENGINE=MyISAM AUTO_INCREMENT=163 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='用户登录记录';