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"
}
相关推荐
冬奇Lab1 天前
AudioRecord音频录制流程深度解析
android·音视频开发·源码阅读
tang777891 天前
小红书平台用什么代理 IP 比较好?2026年3月实测数据 + 选型推荐
网络·爬虫·python·网络协议·tcp/ip·数据挖掘·ip
alexhilton1 天前
Jetpack Compose中的富文本输入
android·kotlin·android jetpack
兄弟加油,别颓废了。1 天前
ctf.show_web4
android
wheelmouse77881 天前
网络排查基础与实战指南:Ping 与 Telnet
开发语言·网络·php
givemeacar1 天前
Nginx如何实现 TCP和UDP代理?
tcp/ip·nginx·udp
OxyTheCrack1 天前
深入浅出TCP拥塞控制技术
网络·tcp/ip
哔哔龙1 天前
Android OpenCV 实战:图片轮廓提取与重叠轮廓合并处理
android·算法
向往着的青绿色1 天前
备份是个好习惯 BugKuCTF题目题解
网络安全·php·安全威胁分析·ctf·安全架构·安全性测试·威胁分析
tangweiguo030519871 天前
Android SSE 流式接收:从手写到框架的进阶之路
android