LEN

PHP GeoIP2 初体验
文档地址: https://github.com/maxmind/GeoIP2-php使用GeoIP2 就一个坑 ...
扫描右侧二维码阅读全文
21
2017/09

PHP GeoIP2 初体验

文档地址: https://github.com/maxmind/GeoIP2-php

使用GeoIP2 就一个坑 就是库从哪里拿. 找了一圈 这篇文章大家可以看一下 http://blog.csdn.net/johnnycode/article/details/42028841

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库做定时更新哦!

Last modification:September 21st, 2017 at 11:36 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment