定制 abivia/geocoding 二次开发

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

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

abivia/geocoding

Composer 安装命令:

composer require abivia/geocoding

包简介

Geocoding support.

README 文档

README

The Abivia geocoding library provides a caching interface to IP address lookup APIs from these sources:

  • ipinfo.io
  • ipstack.com
  • ipapi.co

The library supports both IPv4 and IPv6 addresses and is designed to seamlessly support other IP address geocoding services.

Requirements

Geocoding requires:

  • PHP 8.2 or higher
  • ext-curl to perform API calls
  • abivia/cogs for address support
  • mlocati/ip-lib for IP address support

PHP does not need to be built with IPv6 support.

Installation

Via composer:

composer require abivia/geocoding

Caching

Geocoding comes with two cache handlers designed for light use and a database cache for heavier use. The array cache will retain lookup results for the lifetime of the current request (or session if stored in a session variable). The file cache stores cached data in a text file. Neither offers support for concurrent requests.

The PDO Cache takes a connection to a database, optionally with table names to override the default tables geocoder_cache_ip, geocoder_cache_subnet and geocoder_cache_options. The cache handler will create these tables if they do not already exist.

Applications can create persistent caches by conforming to the CacheHandler interface. Caches support different retention times for lookup hits and lookup misses. In a file cache, the default retention time for a successful lookup is seven days, the default retention time for a failed lookup is one hour. The PDO cache defaults to 30 days for a successful lookup and one hour for a failed lookup.

Cache Purging

Array caches are expected to be transient, so there is no purge logic. The file cache runs a purge at the time the cache is loaded. The PDO cache runs at the interval set by the purgeCacheTime() method, which defaults to 24 hours. On the first object creation, the last purge time is loaded from the database and cached in the session to reduce overhead.

Proxy headers

The static getAddressFromHttp() method now accepts an optional $proxyHeaders parameter. If the parameter is null or not provided, and allowHeaders is true, GeoCoder looks for HTTP_CF_CONNECTING_IP, HTTP_CLIENT_IP, and HTTP_X_FORWARDED_FOR headers, in that order (more proxies may be added as they are discovered). A GeoCoder instance can maintain an independent proxy list.

By default, and address lookup via Geocoder::lookupHttp() checks for the HTTP_X_FORWARDED_FOR header. However, other services such as CloudFlare use non-standard headers. A call to Geocoder::addKnownProxyHeaders() adds all known proxy headers, which are checked for an IP address in order until a valid address is found.

Other proxies can be added or removed by calling Geocoder::addProxyHeader() and Geocoder::removeProxyHeader(). Header names are converted to uppercase. Headers are checked in the reverse order that they are added, so that the most recently added header is checked first.

Sample Usage

use Abivia\Geocode\Geocoder;
use Abivia\Geocode\LookupService\IpInfoApi;

$geocoder = new Geocoder(IpInfoApi::make());
$info = $geocoder->lookup('4.4.4.4');
echo $info->getLatitude() . ', ' . $info->getLongitude();

The geocoder normalizes results from different services using the GeocodeResult interface, but applications can retrieve the service's response through the GeocodeResult::data() method to access extended information.

Example using a file cache

use Abivia\Geocode\CacheHandler\FileCache;
use Abivia\Geocode\Geocoder;
use Abivia\Geocode\LookupService\IpInfoApi;

// Get a cache and set the cache time for a hit to six hours
$cache = new FileCache('mycache.json');
$cache->hitCacheTime(6 * 3600);
$geocoder = new Geocoder(IpInfoApi::make(), $cache);
$info = $geocoder->lookup('4.4.4.4');
echo $info->getLatitude() . ', ' . $info->getLongitude();

Subnet queries

For the most part from a geolocation perspective, only the first 24 bits of an IPv4 and the first 48 of an IPv6 address are significant. Geocoding provides a "subnet" query that will return the last queries result in the v4 or v6 range. This can reduce the number of queries on the lookup service, increasing performance and (in the case of paid services) reducing costs.

Subnet Example

use Abivia\Geocode\Geocoder;
use Abivia\Geocode\LookupService\IpInfoApi;

$geocoder = new Geocoder(IpInfoApi::make());

// Assume 4.4.4.4 is not currently cached. This will cause the service to be queried. 
$info = $geocoder->lookupSubnet('4.4.4.4');
echo $info->getLatitude() . ', ' . $info->getLongitude();

// This query will return the cached data for 4.4.4.4
$info2 = $geocoder->lookupSubnet('4.4.4.8');

Donations welcome

If you're getting something out of Geocoding, you can sponsor us in any amount you wish using Liberapay Liberapay. Liberapay is itself run on donations and takes no fees beyond bank charges.

abivia/geocoding 适用场景与选型建议

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

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

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

围绕 abivia/geocoding 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 51
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-11