一、下载较新的GeoLite2-City.mmdb
【免费】GeoLite2-City.mmdb资源,如果链接过期,可以网上找找最新的城市数据库资源
二、php安装geoip2类库来使用下载下来的数据库
GeoIP2-php 国内镜像库:https://gitcode.com/gh_mirrors/ge/GeoIP2-php
composer require geoip2/geoip2
三、简单使用方法
require 'vendor/autoload.php'; use GeoIp2\Database\Reader; $reader = new Reader('/path/to/GeoLite2-City.mmdb'); $record = $reader->city('8.8.8.8'); // 示例IP echo "国家: " . $record->country->name . "\n";//United States echo "城市: " . $record->country->names['zh-CN'] . "\n";//美国
API链接参考:
http://www.geoplugin.net/json.gp?ip=8.8.8.8