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"
}
相关推荐
岸边的风1 分钟前
无需公网IP的文件交互:FileCodeBox容器化部署技术解析
网络·网络协议·tcp/ip
2501_915374351 小时前
UDP vs TCP:核心差异与应用场景全解析
网络协议·tcp/ip·udp
Edingbrugh.南空1 小时前
操作系统级TCP性能优化:高并发场景下的内核参数调优实践
网络协议·tcp/ip·性能优化
riverz12271 小时前
TCP backlog工作机制
服务器·网络·tcp/ip
Ashlee_code2 小时前
什么是Web3?金融解决方案
开发语言·金融·架构·eclipse·web3·区块链·php
xiangzhihong82 小时前
使用Universal Links与Android App Links实现网页无缝跳转至应用
android·ios
车载应用猿3 小时前
基于Android14的CarService 启动流程分析
android
没有了遇见3 小时前
Android 渐变色实现总结
android
2501_915918416 小时前
接口漏洞怎么抓?Fiddler 中文版 + Postman + Wireshark 实战指南
websocket·网络协议·tcp/ip·http·网络安全·https·udp
雨白6 小时前
Jetpack系列(四):精通WorkManager,让后台任务不再失控
android·android jetpack