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"
}
相关推荐
干啥啥不行,bug第一名1 小时前
EAFNOSUPPORT、EPROTOTYPE、EOPNOTSUPP:socket 的三层参数没有对齐
开发语言·php
好的佩奇3 小时前
Dart 之测试
android·flutter·dart
好的佩奇4 小时前
蓝牙之 GATT 数据操作
android·flutter·蓝牙
三少爷的鞋5 小时前
Android 面试系列之-lifecycleScope 和 CoroutineScope(Dispatchers.Main)
android
Mars-xq7 小时前
Android studio下载很慢download fail
android·ide·android studio
2501_915909067 小时前
iOS 上架 App Store 后推送收不到,APNs 证书和生产沙盒环境排查
android·小程序·https·uni-app·iphone·webview
吃不胖爹8 小时前
安卓原生开发的app如何打包成应用
android
zbguolei9 小时前
第002篇 · 战火中的电子黎明
开发语言·php
YIAN9 小时前
面试串讲:OSI 七层、TCP 三次握手与 LangChain Agent 核心组件
tcp/ip·面试·langchain
ZHOUPUYU10 小时前
PHP 现代工程实践:从语言特性到高性能 Web 应用构建
android·前端·php