符合在yii2框架中使用geoip的扩展

41 篇文章 0 订阅
在main.php中
'ip2location' => [
            'class' => '\client\components\IP2Location\Geolocation',
            'database' => '/data/www/html/httpserver/vendor/ip2location/ip2location-php/databases/IP2LOCATION-LITE-DB1.BIN',
            'mode' => 'FILE_IO',
        ],

实现的代码如下

    /**
     * 根据ip获取地理信息
     * @param  [type] $ip       [需要解析的ip]
     * @param  string $language [返回的地理信息的显示语言  可选 [en zh-CN]]
     * @return [type]           [description]
     */
    public static function getLocationByIp($ip, $language = 'zh-CN')
    {
        $reader = new Reader('/data/www/html/httpserver/data/geoIp/GeoLite2-City.mmdb');
        if (empty($ip) || $ip == '127.0.0.1') {
            return [];
        }
        $citys = $reader->city($ip);
        $country = !empty($citys->country->names) ? $citys->country->names : [];
        $subdivisions = !empty($citys->subdivisions) ? $citys->subdivisions[0]->names : [];
        $city = !empty($citys->city->names) ? $citys->city->names : [];
        $data = [
            'country' => isset($country[$language]) ? $country[$language] : array_shift($country),
            'subdivisions' => isset($subdivisions[$language]) ? $subdivisions[$language] : array_shift($subdivisions),
            'city' => isset($city[$language]) ? $city[$language] : array_shift($city),
        ];
        return $data;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值