php
/** 根据IP地址 获取地理位置*/
function getLocationByIP($ip) {
$url = "http://ip-api.com/json/{$ip}?lang=zh-CN&fields=status,message,country,countryCode,region,regionName,city,lat,lon,timezone,isp,org,as";
$response = file_get_contents($url);
$data = json_decode($response, true);
if ($data['status'] == 'success') {
return $data;
} else {
return ''; // 或者返回错误信息
}
}
在浏览器里请求这个地址
php
http://ip-api.com/json/124.236.132.47?fields=status,message,country,countryCode,region,regionName,city,lat,lon,timezone,isp,org,as&lang=zh-CN
返回了这些信息
php
{
"status":"success",
"country":"中国",
"countryCode":"CN",
"region":"HE",
"regionName":"河北省",
"city":"石家庄市",
"lat":38.036,
"lon":114.47,
"timezone":"Asia/Shanghai",
"isp":"Chinanet",
"org":"Chinanet HE",
"as":"AS4134 CHINANET-BACKBONE"
}