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"
}
相关推荐
帅次9 小时前
Android 高级工程师面试:Java 基础知识 近1年高频追问 22 题
android·java·面试
周小码10 小时前
10分钟搭建管理后台:laravel-admin实战入门
php·laravel
私人珍藏库10 小时前
[Android] zip解压缩管理-全格式压缩包一键解压+打包
android·app·生活·工具·多功能
dog25010 小时前
从重尾到截断流量模型的演进
开发语言·php
雨白10 小时前
C语言:动态内存分配
android
Android-Flutter11 小时前
android compose 自定义Painter绘制图形 使用
android·kotlin·compose
我是一颗柠檬11 小时前
【Java项目技术亮点】覆盖索引与索引下推优化
android·java·开发语言
vigor51212 小时前
MySQL通过Mango实现分库分表
android·数据库·mysql
huainingning13 小时前
华三ACL单向TCP互通组网-通过Established状态回包实现
运维·网络·tcp/ip