yzh52521/think-geoip

支持多种GeoIP服务。

v1.0.5 2023-02-09 03:50 UTC

This package is auto-updated.

Last update: 2024-09-09 07:19:14 UTC


README

根据网站访问者的IP地址确定其地理位置

安装

composer require yzh52521/think-geoip

配置配置文件的两种主要选项的快速概述。要了解更多信息,请打开config/geoip.php文件。

服务配置为了简化并保持整洁,所有为服务所需的三方composer包都分别安装。

有关更多配置选项,请查看服务页面。

缓存配置GeoIP使用think默认缓存来存储查询的IP位置。这样做是为了减少对所选服务的调用次数,因为其中一些服务有速率限制。

选项

all 所有位置都缓存一些只缓存请求用户没有缓存完全禁用

基本用法

使用这些方法最简单的方式是通过helper函数geoip()或通过使用facade \yzh52521\GeoIP\facades\GeoIP。以下示例我们将使用helper方法。

获取网站访问者的位置数据:

geoip($ip = null);

参数: $ip - 要查找的IP。如果没有设置,则应用程序默认为远程地址。示例:

\yzh52521\GeoIP\Location {

    #attributes:array [
        'ip'           => '232.223.11.11',
        'iso_code'     => 'US',
        'country'      => 'United States',
        'city'         => 'New Haven',
        'state'        => 'CT',
        'state_name'   => 'Connecticut',
        'postal_code'  => '06510',
        'lat'          => 41.28,
        'lon'          => -72.88,
        'timezone'     => 'America/New_York',
        'continent'    => 'NA',
        'currency'     => 'USD',
        'default'      => false,
    ]
}

默认位置在找不到位置的情况下,将返回回退位置,默认参数设置为true。要设置自己的默认值,请在配置文件config/geoip.php中进行更改

think命令

发布服务数据

php think geoip:publish

更新服务数据某些服务可能需要更新本地文件。例如,MaxMind数据库服务从远程数据库中获取并保存到本地文件系统中。

php think geoip:update

清除缓存位置一些缓存驱动程序提供清除存储位置的能力。

php think geoip:clear

方法

当使用geoip()辅助函数不带参数时,它将返回\Torann\GeoIP\GeoIP实例,然后我们可以做所有类型的神奇事情。

getLocation($ip = null)

从提供的IP获取位置。

参数

$ip - 要查找的IP。如果没有设置,则应用程序默认为远程地址。

geoip()->getLocation('27.974.399.65');

getService()将返回用于确定位置的默认服务。

getClientIP()

将返回用户IP地址。

服务

服务前提条件在使用MaxMind驱动程序之前,您需要通过Composer安装适当的包。

MaxMind: geoip2/geoip2 ~2.1 IP-API(默认)他们提供免费和付费服务ip-api.com

'service' => 'ipapi',

MaxMind数据库在配置文件中指定要使用的数据库位置,在services部分的maxmind_database下。同时指定在运行php artisan geoip:update时从何处下载数据库的URL。注意:在包工作之前,需要运行geoip:update命令。

'service' => 'maxmind_database',

优化提示:当使用数据库选项时,我不希望在git仓库中保留下载的数据库,所以我让部署系统在部署到服务器之前在构建过程中运行geoip:update。

MaxMind API在www.maxmind.com注册以获取许可证密钥和用户ID。

'service' => 'maxmind_api',

IPGEOLOCATION API在ipgeolocation.io注册以获取API密钥,并将其添加到您的env文件中,如下所示:IPGEOLOCATION_KEY = YOUR_API_KEY

'service' => 'ipgeolocation',

IPFINDER API在ipfinder.io注册以获取API密钥,并将其添加到您的env文件中,如下所示:IPFINDER_API_KEY = YOUR_API_KEY

'service' => 'ipfinder',

IPDATA API在ipdata.co注册以获取API密钥,并将其添加到您的env文件中,如下所示:IPDATA_API_KEY = YOUR_API_KEY

'service' => 'ipdata',

自定义服务存储在GeoIP的配置文件config/geoip.php中。只需更新服务名称为您的自定义服务名称,并将其添加到特定配置部分的类值中,类值为自定义类名。

示例服务

<?php

namespace App\GeoIP\Services;

use Exception;
use yzh52521\GeoIP\support\HttpClient;
use yzh52521\GeoIP\services\AbstractService;

class FooBar extends AbstractService
{
    /**
     * Http client instance.
     *
     * @var HttpClient
     */
    protected $client;

    /**
     * The "booting" method of the service.
     *
     * @return void
     */
    public function boot()
    {
        $this->client = new HttpClient([
            'base_uri' => 'http://api.foobar-ip-to-location.com/',
            'query' => [
                'some_option' => $this->config('some_option'),
            ],
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function locate($ip)
    {
        // Get data from client
        $data = $this->client->get('find', [
            'ip' => $ip,
        ]);

        // Verify server response
        if ($this->client->getErrors() !== null) {
            throw new Exception('Request failed (' . $this->client->getErrors() . ')');
        }

        // Parse body content
        $json = json_decode($data[0]);

        return [
            'ip' => $ip,
            'iso_code' => $json->iso_code,
            'country' => $json->country,
            'city' => $json->city,
            'state' => $json->state,
            'state_name' => $json->state_name,
            'postal_code' => $json->postal_code,
            'lat' => $json->lat,
            'lon' => $json->lon,
            'timezone' => $json->timezone,
            'continent' => $json->continent,
        ];
    }

    /**
     * Update function for service.
     *
     * @return string
     */
    public function update()
    {
        // Optional artisan command line update method
    }
}

在配置文件中

'service' => 'foobar',

'services' => [

    ...

    'foobar' => [
        'class' => \App\GeoIP\Services\FooBar::class,
        'some_option'  => 'some_option_value',
    ],

],