function getCity($ip) {
// 获取当前位置所在城市
$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=2TGbi6zzFm5rjYKqPPomh9GBwcgLW5sS&ip={$ip}&coor=bd09ll");
$json = json_decode($content);
$address = $json->{'content'}->{'address'};//按层级关系提取address数据
$data['address'] = $address;
$res = [];
$res['province'] = mb_substr($data['address'],0,3,'utf-8');
$res['city'] = mb_substr($data['address'],3,3,'utf-8');
return $res;
}
$ip = '217.478.28.12';
dump(getCity($ip));
打印结果为:province=>'河南省', 'city'=>'郑州市'