PHP根据IP地址获取地理位置城市和经纬度信息

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"
}
相关推荐
深念Y8 小时前
RIO-UL00(EMUI 4.1 / Android 6.0.1 / arm64)开机自启动 sshd
android·linux·华为·安卓·chroot·sshd·emui
爱和冰阔落8 小时前
【MySQL 慢查询排查实战】列表接口逐渐变慢时,怎样从请求链路定位原因
android·数据库·mysql
dog2508 小时前
多路径聚合效率的统计理论
网络·tcp/ip
剑锋所指,所向披靡!9 小时前
TCP并发服务器
服务器·网络·tcp/ip
前进的程序员9 小时前
告别Copilot?Codex本地化部署指南
开源·php·copilot·codex
weixin_BYSJ19879 小时前
springboot智慧公共交通系统---附源码51123
java·javascript·spring boot·python·django·flask·php
暴力求解20 小时前
Linux网络---传输层协议TCP(二)
linux·服务器·开发语言·网络·tcp/ip
80s7771 天前
动态ip和静态ip有什么区别
服务器·网络·tcp/ip
hunterandroid1 天前
Android WebView JSBridge 治理实战:从线上白屏崩溃到协议化通信
android·前端