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"
}
相关推荐
ll_god13 分钟前
android compose ui 结合 ViewModel适配方案
android·ui
2501_9481201516 分钟前
Android智能手机信息安全管理系统的研究
android·智能手机
DengDongQi30 分钟前
Android Compose 应用中实现全局Dialog管理器的设计与实践
android
容华谢后33 分钟前
Android消息推送 MQTT方案实践
android
橘颂TA1 小时前
【Linux 网络】从理论到实践:IP 协议的报头分析与分段技术详解
linux·运维·服务器·网络·tcp/ip
_李小白1 小时前
【Android 美颜相机】第十五天:GPUImage3x3TextureSamplingFilter 解析
android·数码相机
Whisper_Sy1 小时前
Flutter for OpenHarmony移动数据使用监管助手App实战 - 月报告实现
android·开发语言·javascript·网络·flutter·ecmascript
天才少年曾牛1 小时前
Android 怎么写一个AIDL接口?
android
迎仔2 小时前
计算机网络分层模型通俗指南 (OSI vs TCP/IP)
网络协议·tcp/ip·计算机网络
冬奇Lab2 小时前
【Kotlin系列14】编译器插件与注解处理器开发:在编译期操控Kotlin
android·开发语言·kotlin·状态模式