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"
}
相关推荐
石山代码16 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·人工智能
__log18 小时前
弱网环境下的“生命线“:从AI流式响应到大文件上传的稳定性
开发语言·人工智能·php
怣疯knight18 小时前
kotlin安卓应用打包编译卡死的可能原因
android·kotlin
plainGeekDev18 小时前
NullPointerException → Kotlin 空安全
android·java·kotlin
TlSfoward18 小时前
TLSFOWARD TLS指纹
开发语言·数据库·爬虫·搜索引擎·https·php
CHANG_THE_WORLD20 小时前
6.多线程的TCP通信
java·网络·tcp/ip
信仰87420 小时前
Linux进阶篇01:网络基础配置
linux·网络·php
2501_9160088920 小时前
苹果上架工具怎么选 不用 Mac 上架 App Store 的几种方案
android·macos·ios·小程序·uni-app·iphone·webview
Qlittleboy21 小时前
PHP的接口参数传递的过多,max_input_vars = 5000
开发语言·php
jijihusong0061 天前
KMP全栈开发教程:从Android到AI Agent
android·人工智能