torann/geoip通过该扩展包,我们可以根据用户的IP获得地理位置信息。
项目地址:https://github.com/Torann/laravel-geoip
安装命令:
$ composer require torann/geoip
安装完成之后,我们将配置文件发布出来:
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config
生成config/geoip.php文件
我们使用tinker进行测试
$php artisan tinker
>>> $ip = '119.4.121.109';
=>"119.4.121.109"
>>> geoip($ip);
=>Torann\GeoIP\Location {$2852}
我们得到一个对象
>>>getip($ip)->toArray();

我们使用toArray获得对象中的数据信息。
通过结果我们发现返回的数据是英文的,我们需要中文的数据,打开配置文件config/geoip.php。
找到:
'service' => 'ipapi',
将ipapi中的
'lang' => 'en', 修改为'lang' => 'zh-CN',

我们重启tinker,清理缓存信息。
$php artisan cache:clear
再次执行geoip的ip测试代码,我们获得了中文的信息。