shlinkio/shlink-ip-geolocation
Composer 安装命令:
composer require shlinkio/shlink-ip-geolocation
包简介
Shlink module with tools to geolocate an IP address using different strategies.
README 文档
README
Shlink module with tools to geolocate an IP address using different strategies.
Most of the elements it provides require a PSR-11 container, and it's easy to integrate on mezzio applications thanks to the ConfigProvider it includes.
Install
Install this library using composer:
composer require shlinkio/shlink-ip-geolocation
This library is also an expressive module which provides its own
ConfigProvider. Add it to your configuration to get everything automatically set up.
Resolving IP locations
This module provides a few different ways to resolve the location of an IP address, all of them implementing Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface.
This interface basically exposes this method:
/** * @throws WrongIpException */ public function resolveIpLocation(string $ipAddress): Model\Location;
These are the strategies provided implementing this interface:
GeoLite2LocationResolver: It makes use of a GeoLite2 database to resolve IP address locations.EmptyIpLocationResolver: This one is a dummy resolver which always returns an empty location as if it was not capable of resolving the address location.CainIpLocationResolver: It wraps a list of IP resolvers and calls them sequentially until one of them is capable of resolving the address location.
The first one is the most reliable, but requires an up-to-date GeoLite2 database (which handling is explained in next section).
However, the chain resolver encapsulates the other two in the order they are listed here, and is the one recommended to use.
It is aliased to the service with name Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface.
Handling GeoLite2 DB file
In order to resolve IP address locations with the GeoLite2LocationResolver, an up-to-date local GeoLite2 database file.
To ease the management of this file, a Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdater service is provided.
It exposes two public methods:
public function databaseFileExists(): bool; /** * @throws RuntimeException */ public function downloadFreshCopy(?callable $handleProgress = null): void;
databaseFileExists: Just tells if the database file exists already (either in an outdated or up to date form).downloadFreshCopy: Forces a new copy of the GeoLite2 database to be downloaded from MaxMind repos. It allows to optionally handle the progress of the download.
GeoLite2 config
To get both the resolver and the database updater to work, this configuration has to be defined:
<?php declare(strict_types=1); return [ 'geolite2' => [ // Mandatory options 'db_location' => __DIR__ . '/../../data/GeoLite2-City.mmdb', 'temp_dir' => sys_get_temp_dir(), // Optional options 'license_key' => 'kjhk45hkj34fdwe5', // If not set, no attempts to download the file will happen ], ];
db_location: Tells where in the local filesystem the database file is located (or should be located once theDbUpdaterdownloads it).temp_dir: A temporary location where new versions of the database are located while downloading. Once a download succeeds, the new DB will be moved to the location defined in previous config option.license_key: The GeoLite license key used to download the database. Know how to generate your own.
This project includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com
shlinkio/shlink-ip-geolocation 适用场景与选型建议
shlinkio/shlink-ip-geolocation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75.06k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2019 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 shlinkio/shlink-ip-geolocation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shlinkio/shlink-ip-geolocation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 75.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-12