使用各种服务(在线和本地)从访客的 IP 地址检索其位置。
使用composer require
安装位置:
composer require stevebauman/location
发布配置文件(这将在config
目录中创建一个location.php
文件):
php artisan vendor:publish --provider= " StevebaumanLocationLocationServiceProvider "
use Stevebauman Location Facades Location ;
if ( $ position = Location:: get ()) {
// Successfully retrieved position.
echo $ position -> countryName ;
} else {
// Failed retrieving position.
}
重要的:
- 此方法通过
request()->ip()
检索用户的 IP 地址。- 在默认配置中,
testing.enabled
处于活动状态,返回的 IP 地址位于美国。禁用它以获取客户端的真实IP地址。
$ position = Location:: get ( ' 192.168.1.1 ' );
您可以使用一组 IP 地址模式及其预期位置来调用Location::fake
来伪造 IP 地址的位置:
use Stevebauman Location Position ;
use Stevebauman Location Facades Location ;
Location:: fake ([
' 127.0.0.1 ' => Position:: make ([
' countryName ' => ' United States ' ,
' countryCode ' => ' US ' ,
// ...
])
]);
// Somewhere in your application...
$ position = Location:: get ( ' 127.0.0.1 ' ); // Position
如果您愿意,可以使用星号为给定的任何 IP 地址返回相同的位置:
Location:: fake ([
' * ' => Position:: make ([
' countryName ' => ' United States ' ,
' countryCode ' => ' US ' ,
// ...
])
]);
$ position = Location:: get ( $ anyIpAddress ); // Position
如果没有给出期望,或者期望不匹配, Location::get
将返回false
:
Location:: fake ();
Location:: get ( $ anyIpAddress ); // false
如果您的应用程序尝试检索多个 IP 地址的位置,您可以提供多个 IP 地址期望模式:
Location:: fake ([
' 127.0.0.1 ' => Position:: make ([
' countryName ' => ' United States ' ,
' countryCode ' => ' US ' ,
// ...
]),
' 192.168.1.1 ' => Position:: make ([
' countryName ' => ' Canada ' ,
' countryCode ' => ' CA ' ,
// ...
]),
]);
可用的驱动程序:
鼓励使用本地数据库将 MaxMind 设置为后备驱动程序,以便在达到外部 Web 服务的速率限制时返回一些位置信息。
要设置 MaxMind 从您自己的服务器检索用户的位置,您必须:
MAXMIND_LICENSE_KEY
密钥将其保存到您的.env
文件中php artisan location:update
将最新的.mmdb
文件下载到您的database/maxmind
目录中注意:请记住,您需要通过定期运行
location:update
来更新此文件,以从访问者那里检索最新信息。
在配置文件中,您可以根据需要指定任意数量的后备驱动程序。建议使用本地数据库.mmdb
文件(如上所述)配置 MaxMind 驱动程序,以便您始终从访问者那里检索一些通用位置信息。
如果尝试获取驱动程序时发生异常(例如,如果提供商 API 发生更改,则会出现 400/500 错误),它将自动使用队列中的下一个驱动程序。
要创建您自己的驱动程序,只需在应用程序中创建一个类,并扩展抽象驱动程序:
namespace App Location Drivers ;
use Illuminate Support Fluent ;
use Illuminate Support Facades Http ;
use Stevebauman Location Position ;
use Stevebauman Location Request ;
use Stevebauman Location Drivers Driver ;
class MyDriver extends Driver
{
protected function process ( Request $ request ): Fluent
{
$ response = Http:: get ( " https://driver-url.com " , [ ' ip ' => $ request -> getIp ()]);
return new Fluent ( $ response -> json ());
}
protected function hydrate ( Position $ position , Fluent $ location ): Position
{
$ position -> countryCode = $ location -> country_code ;
return $ position ;
}
}
然后,将您的驱动程序类名插入配置文件中:
// config/location.php
' driver ' => App Location Drivers MyDriver::class,
在版本 7 中,代码库已更新为严格的 PHP 类型、更新的 PHP 和 Laravel 版本要求、更新的Driver
结构以及少量的配置添加。
在版本 7 中,位置驱动程序现在可以实现Updatable
接口,允许使用location:update
命令更新它们。目前,只有 MaxMind 驱动程序支持此功能。
要更新您的配置文件以便能够自动下载最新的 MaxMind 数据库文件,请在config/location.php
文件中插入以下url
配置选项:
// config/location.php
return [
'maxmind' => [
// ...
'local' => [
// ...
+ 'url' => sprintf('https://download.maxmind.com/app/geoip_download_by_token?edition_id=GeoLite2-City&license_key=%s&suffix=tar.gz', env('MAXMIND_LICENSE_KEY')),
],
],
];
完成后,您可以执行以下 artisan 命令来下载最新的.mmdb
文件:
php artisan location:update
在版本 7 中,代码库已更新为严格的 PHP 类型、更新的 PHP 和 Laravel 版本要求以及更新的Driver
结构。
如果您创建了自己的自定义驱动程序实现,则必须将其更新为使用基本Driver
或HttpDriver
类。
如果您使用 HTTP 服务获取位置,则扩展HttpDriver
以减少需要编写的代码可能会很有用:
namespace AppLocationDrivers;
use IlluminateSupportFluent;
use StevebaumanLocationPosition;
- use StevebaumanLocationDriversDriver;
+ use StevebaumanLocationDriversHttpDriver;
- class MyDriver extends Driver
+ class MyDriver extends HttpDriver
{
- public function url($ip)
+ public function url(string $ip): string;
{
return "http://driver-url.com?ip=$ip";
}
- protected function process($ip)
- {
- return rescue(function () use ($ip) {
- $response = json_decode(file_get_contents($this->url($ip)), true);
-
- return new Fluent($response);
- }, $rescue = false);
- }
- protected function hydrate(Position $position, Fluent $location)
+ protected function hydrate(Position $position, Fluent $location): Position;
{
$position->countryCode = $location->country_code;
return $position;
}
}
位置尽可能根据语义版本控制指南进行版本控制。
版本将按以下格式编号:
<major>.<minor>.<patch>
并按照以下准则构建:
次要版本不会单独维护,我们鼓励您升级到下一个次要版本。
主要版本通过单独的分支单独维护。