定制 yzh52521/think-geoip 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yzh52521/think-geoip

Composer 安装命令:

composer require yzh52521/think-geoip

包简介

Support for multiple GeoIP services.

README 文档

README

Determine the geographical location of website visitors based on their IP addresses

install

composer require yzh52521/think-geoip

Configuration Quick breakdown of the two main options in the configuration file. To find out more simple open the config/geoip.php file.

Service Configuration To simplify and keep things clean, all third party composer packages, that are needed for a service, are installed separately.

For further configuration options checkout the services page.

Caching Configuration GeoIP uses think default caching to store queried IP locations. This is done to reduce the number of calls made to the selected service, as some of them are rate limited.

Options:

all all location are cached some cache only the requesting user none caching is completely disable

基本用法

The simplest way to use these method is through the helper function geoip() or by using the facade \yzh52521\GeoIP\facades\GeoIP. For the examples below we will use the helper method.

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

geoip($ip = null);

Arguments: $ip - The Ip to look up. If not set the application default to the remote address.示例:

\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,
    ]
}

Default Location In the case that a location is not found the fallback location will be returned with the default parameter set to true. To set your own default change it in the configurations config/geoip.php

think Commands

publish Service Data

php think geoip:publish

Updating Service Data Some services may need to update local files. For example the MaxMind Database service fetches a remote database and saves it to the local file system.

php think geoip:update

Clearing Cached Locations Some cache drivers offer the ability to clear stored location.

php think geoip:clear

Methods

When the geoip() helper function is used without arguments it will return the \Torann\GeoIP\GeoIP instance, and with this we can do all types of amazing things.

getLocation($ip = null)

Get the location from the provided IP.

Arguments:

$ip - The Ip to look up. If not set the application default to the remote address.

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

getService() Will return the default service used for determining location.

getClientIP()

Will return the user IP address.

Services

Service Prerequisites Before using the MaxMind driver, you will need to install the appropriate package via Composer:

MaxMind: geoip2/geoip2 ~2.1 IP-API (default) They offer a free and pro service ip-api.com

'service' => 'ipapi',

MaxMind Database The database location to used is specified in the config file in the services section under maxmind_database. Along with the URL of where to download the database from when running the php artisan geoip:update. Note: The geoip:update command will need to be ran before the package will work.

'service' => 'maxmind_database',

Optimization Tip: When using the database option I don't like having the downloaded database in my git repository, so I have my deploy system run the geoip:update during the build process before it's deployed to the servers.

MaxMind API Register for a license key and user ID at www.maxmind.com

'service' => 'maxmind_api',

IPGEOLOCATION API Register at ipgeolocation.io to get api key and add it into your env file as: IPGEOLOCATION_KEY = YOUR_API_KEY

'service' => 'ipgeolocation',

IPFINDER API Register at ipfinder.io to get api key and add it into your env file as: IPFINDER_API_KEY = YOUR_API_KEY

'service' => 'ipfinder',

IPDATA API Register at ipdata.co to get api key and add it into your env file as: IPDATA_API_KEY = YOUR_API_KEY

'service' => 'ipdata',

Custom Service Services are stored in the GeoIP's config file config/geoip.php. Simply update the service with the name of your custom service and add it to the services specific configuration section with the class value as the custom classname.

Example service

<?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
    }
}

In the config file

'service' => 'foobar',

'services' => [

    ...

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

],

yzh52521/think-geoip 适用场景与选型建议

yzh52521/think-geoip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「geolocation」 「maxmind」 「geoip」 「location」 「thinkphp」 「IP API」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yzh52521/think-geoip 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yzh52521/think-geoip 我们能提供哪些服务?
定制开发 / 二次开发

基于 yzh52521/think-geoip 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 57
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2022-08-29