定制 timefrontiers/php-location 二次开发

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

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

timefrontiers/php-location

Composer 安装命令:

composer require timefrontiers/php-location

包简介

Get visitor's location information from IP address with multiple provider support

README 文档

README

A modern, flexible PHP library to retrieve visitor location information from IP address with support for multiple GeoIP providers.

PHP Version License

Features

  • Multiple GeoIP provider support via interface – easily switch between services.
  • Built-in free provider using ip-api.com (no API key required).
  • Currency symbol mapping for over 100 currencies.
  • Client IP auto-detection from common server headers.
  • Error collection with access‑based filtering via InstanceError.
  • Strict typing and modern PHP (8.1+).
  • PSR‑4 autoloading.

Installation

composer require timefrontiers/php-location

Requirements

Basic Usage

Quick Start

use TimeFrontiers\Location;

$location = new Location();

echo $location->ip;              // 123.45.67.89
echo $location->city;            // Lagos
echo $location->state;           // Lagos
echo $location->country;         // Nigeria
echo $location->country_code;    // NG
echo $location->currency_code;   // NGN
echo $location->currency_symbol; // ₦
echo $location->latitude;        // 6.4474
echo $location->longitude;       // 3.3903

Specify an IP Address

$location = new Location('8.8.8.8');
echo $location->city; // Mountain View

Refresh Location

$location = new Location();
// Later...
$location->refresh('1.1.1.1');

Error Handling

Errors are stored in a protected $_errors property and can be retrieved with getErrors(). Use the InstanceError package to filter errors based on user rank.

use TimeFrontiers\Location;
use TimeFrontiers\InstanceError;

$location = new Location('invalid-ip');

if (!$location->refresh()) {
    $errors = (new InstanceError($location, false))->get();
    foreach ($errors['refresh'] as $error) {
        echo $error[2]; // Error message
    }
}

GeoIP Providers

Default: ip-api.com (free)

The library uses ip-api.com by default with no API key required. Free tier limit: 45 requests per minute.

$location = new Location(); // Uses ip-api.com

Using a Commercial ip-api.com Key

use TimeFrontiers\GeoIP\IpApiService;

$service = new IpApiService('your-api-key');
$location = new Location(null, $service);

Creating a Custom Provider

Implement the GeoIPInterface and pass it to the constructor.

use TimeFrontiers\GeoIP\GeoIPInterface;
use TimeFrontiers\GeoIP\LocationData;
use TimeFrontiers\Location;

class MyProvider implements GeoIPInterface
{
    public function locate(string $ip): LocationData
    {
        // Fetch from your API
        return new LocationData(
            ip: $ip,
            city: '...',
            region: '...',
            country: '...',
            country_code: '...',
            currency_code: '...',
            currency_symbol: '...',
            latitude: 0.0,
            longitude: 0.0
        );
    }
}

$location = new Location(null, new MyProvider());

Currency Symbols

The library includes a static map of over 100 currency codes to symbols (CurrencySymbols::get('USD') // '$'). If a code is not found, the code itself is returned.

Available Properties

Property Type Description
$ip string IP address used
$city string City name
$city_code ?string Reserved for future use (currently null)
$state string Region/state name
$state_code ?string Reserved for future use (currently null)
$country string Country name
$country_code string Two‑letter country code
$currency_code string Three‑letter currency code
$currency_symbol string Currency symbol (e.g. $, €, ₦)
$latitude float Latitude
$longitude float Longitude

Security Considerations

  • IP addresses are never stored locally; they are sent to the configured GeoIP provider.
  • When using a free service, be aware of rate limits and privacy policies.
  • Client IP detection respects proxy headers; ensure your application is configured securely.

License

MIT License. See LICENSE for details.

timefrontiers/php-location 适用场景与选型建议

timefrontiers/php-location 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 timefrontiers/php-location 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 28
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 42
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-14