evinkuraga / geolocation
一个用于集成IP Info DB服务的Laravel包
Requires
- php: >=5.5.9
- guzzlehttp/guzzle: ^6.2|~7.0
- laravel/framework: ^5.0|^6.0|^7.0
- mockery/mockery: ^1.0
Requires (Dev)
- phpunit/phpunit: ^6.5
- satooshi/php-coveralls: 1.0.*
- sempro/phpunit-pretty-print: ^1.0
README
Laravel的IP Info DB集成
注意
这个包是从evinkuraga/geolocation分支出来的,这样我才能编辑composer文件以接受guzzle ^7.0,这是与Laravel 6.0的后续版本一起工作的必要条件。我已经注意到Guzzle的使用很少,所以我最终会调整它以使用Guzzle 7,或者直接添加到composer中看看会发生什么。
安装
此包需要PHP 5.6+,并包含Laravel 5服务提供者和外观。
要通过composer安装,请将包包含在您的composer.json
中。
"evinkuraga/geolocation": "^2.0"
运行composer install
或composer update
以下载依赖项,或者您可以运行composer require evinkuraga/geolocation
。
刷新自动加载器
此时,一些用户可能需要运行命令composer dump-autoload
。或者,您可以运行php artisan optimize
,它应该包含dump-autoload命令。
Laravel 5集成
要使用此包与Laravel 5一起使用,首先将GeoLocation服务提供者添加到app/config/app.php
中的服务提供者列表中。
'providers' => [
Evinkuraga\GeoLocation\GeoLocationServiceProvider::class
];
将GeoLocation
外观添加到您的别名字符数组中。
'aliases' => [
'GeoLocation' => Evinkuraga\GeoLocation\Facades\GeoLocation::class,
];
使用php artisan vendor:publish --provider="Evinkuraga\GeoLocation\GeoLocationServiceProvider"
发布配置和迁移文件。
配置文件
发布配置文件后,您将在config
文件夹中找到一个geolocation.php
文件。您应该查看这些设置,并在必要时进行更新。
环境变量
您需要将以下内容添加到您的.env
文件中,并使用自己的设置更新这些内容
GEOLOCATION_API_KEY=<key>
GEOLOCATION_CACHE=<duration_in_minutes>
获取您的GeoLocation API密钥
在使用此包之前,您必须从IP Info DB获取一个API密钥。请访问http://ipinfodb.com/register.php进行注册并确认您的电子邮件地址后,您的API密钥将显示出来。请复制并设置到您的.env
文件中的GEOLOCATION_API_KEY
选项。
示例用法
use Evinkuraga\GeoLocation\Contracts\Services\GeoLocation;
use Illuminate\Http\Request;
public function index(GeoLocation $geo, Request $request)
{
$ipLocation = $geo->getCity($request->ip());
// if you do $geo->get($request->ip()), the default precision is now city
// $ipLocation is an IpLocation Object
echo $ipLocation->ipAddress; // e.g. 127.0.0.1
echo $ipLocation->getAddressString(); // e.g. London, United Kingdom
// the object has a toJson() and toArray() method on it
// so you can die and dump an array.
dd($ipLocation->toArray());
}
IpLocation的方法
$ipLocation->getStatusCode(); // returns status code of request (e.g. 200)
$ipLocation->getStatusMessage(); // returns any status message to go with code
$ipLocation->getIpAddress(); // the geolocation IP requested
$ipLocation->getCountryCode(); // country code of the IP e.g. GB
$ipLocation->getCountryName(); // country name of the IP e.g. United Kingdom
$ipLocation->getRegionName(); // region name of the IP e.g. England
$ipLocation->getCityName(); // city name of the IP e.g. London
$ipLocation->getZipCode(); // postcode of the IP e.g. SE01 1AA
$ipLocation->getPostCode(); // postcode of the IP e.g. SE01 1AA
$ipLocation->getLatitude(); // latitude of the IP e.g. 53.4030
$ipLocation->getLongitude(); // longitude of the IP e.g. -1.201
$ipLocation->getTimeZone(); // timezone of the IP e.g.
$ipLocation->getAddressString(); // gets the city, region and country as a string
$ipLocation->toArray(); // returns object as an array
$ipLocation->toJson(); // returns object as a json object