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"
}
相关推荐
ooseabiscuit11 分钟前
Laravel5
android·php·laravel
科技道人1 小时前
Android 禁止使用ipv6 测试
android·禁用ipv6
AlexMaybeBot1 小时前
巧用 OpenClaw 为 Android 开发电脑瘦身
android·github·ai编程
wangl_922 小时前
Modbus RTU 与 Modbus TCP 深入指南-决策树与选型建议
网络·网络协议·tcp/ip·tcp·modbus·rtu
pengyi8710152 小时前
共享IP全面优缺点解析,适合什么人群使用?
linux·运维·服务器·网络·tcp/ip
m0_738120724 小时前
ctfshow靶场SSRF部分——基础绕过到协议攻击解题思路与技巧(二)
python·网络协议·tcp/ip·安全·网络安全
神仙别闹4 小时前
基于PHP+MySQL实现在线考试系统
开发语言·mysql·php
funnycoffee1235 小时前
Cisco Firewpower 4100 9300 FXOS change management ip address
linux·数据库·tcp/ip
pengyu5 小时前
【Kotlin 协程修仙录 · 金丹境 · 中阶】 | 启动密法:CoroutineStart 四种模式与底层调度玄机
android·kotlin
wangl_925 小时前
Modbus RTU 与 Modbus TCP 深入指南-结束语
网络·网络协议·tcp/ip·tcp·modbus·rtu