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"
}
相关推荐
wqdian_com12 分钟前
中文域名的准确展示能否堵住网络钓鱼攻击“后门”?
服务器·安全·php
消失的旧时光-19431 小时前
从 Android 组件化到 Flutter 组件化
android·flutter·架构
ckjr0071 小时前
如何通过内容打造IP?
网络·网络协议·tcp/ip
Android轮子哥2 小时前
Android 12 SplashScreen 一种另类的适配方案
android·github
YJlio2 小时前
PsPing 学习笔记(14.7):一条龙网络体检脚本——连通性、延迟、带宽全都要
开发语言·网络·笔记·python·学习·pdf·php
粤M温同学2 小时前
Android OkHttp 下载限速方案实现
android·okhttp
2501_915106323 小时前
Perfdog 成本变高之后,Windows 上还能怎么做 iOS APP 性能测试
android·ios·小程序·https·uni-app·iphone·webview
勇气要爆发4 小时前
内网 IP 怎么访问互联网?NAT 技术与“小区保安”的比喻
网络·网络协议·tcp/ip·nat
愤怒的代码4 小时前
从开发调试到生产上线:全维度 Android 内存监控与分析体系构建
android·java·kotlin
jzlhll1234 小时前
Android最简化发布模块到mavenCentral
android·mavencentral