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"
}
相关推荐
许杰小刀4 小时前
ctfshow-web文件包含(web78-web86)
android·前端·android studio
韭菜钟5 小时前
WIndows下一键切换网卡IP脚本
windows·网络协议·tcp/ip
上海云盾-小余9 小时前
DDoS 攻击全解析:常见类型识别与分层防御思路
网络协议·tcp/ip·安全·ddos
恋猫de小郭9 小时前
Android 上为什么主题字体对 Flutter 不生效,对 Compose 生效?Flutter 中文字体问题修复
android·前端·flutter
三少爷的鞋9 小时前
不要让调用方承担你本该承担的复杂度 —— Android Data 层设计原则
android
卤炖阑尾炎9 小时前
Python 网络编程实战:从 TCP/UDP 基础到高并发服务器开发
网络·python·tcp/ip
李李李勃谦10 小时前
Flutter 框架跨平台鸿蒙开发 - 创意灵感收集
android·flutter·harmonyos
W.W.H.10 小时前
嵌入式常见的面试题1
linux·网络·经验分享·网络协议·tcp/ip
zmj32032410 小时前
CAN + 以太网 + Wi-Fi + BLE + TCP/IP + MQTT +HTTP协议层级
网络·网络协议·tcp/ip