public function getCityInfo()
{
// This creates the Reader object, which should be reused across
// lookups.
$city_reader = new Reader('/mnt/data/geolite2/GeoLite2-City.mmdb');
// Replace "city" with the appropriate method for your database, e.g.,
// "country".
$record = $city_reader->city($_SERVER['REMOTE_ADDR']);
print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
print($record->country->names['zh-CN'] . "\n"); // '美国'
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
print($record->city->name . "\n"); // 'Minneapolis'
print($record->postal->code . "\n"); // '55455'
print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
}
输出 :CN
China
中国
Beijing
11
Beijing
39.9289
116.3883
用起来还好 根据IP获取地域信息, 库信息获取分为免费和商业的, 免费的精度没有验证. 有条件建议大家使用商业的吧. 官方每月都会更新一次. ip库做定时更新哦!