grebennik/ip-clock
Composer 安装命令:
composer require grebennik/ip-clock
包简介
A PSR-20 Clock implementation that returns time based on server external IP address using external API.
README 文档
README
A Composer package that returns accurate time and timezone based on a server’s external IP address. Implements \Psr\Clock\ClockInterface (PSR-20).
Requirements
- PHP 8.2 or higher
- Composer
Installation
Install the package via Composer:
composer require grebennik/ip-clock
Note: If the package is not yet published on Packagist, add a VCS repository to your composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/your-username/ip-clock"
}
]
}
Usage
Automatically use the server’s external IP
use Grebennik\IpClock\IpClock; $clock = new IpClock(); $now = $clock->now(); // Returns DateTimeImmutable echo $now->format('Y-m-d H:i:s P');
Use a specific IP address
use Grebennik\IpClock\IpClock; $clock = new IpClock('8.8.8.8'); $now = $clock->now(); echo "Time for 8.8.8.8: " . $now->format('H:i:s');
Data source
By default, the package uses WorldTimeAPI. This service is free and does not require an API key.
Using Authenticated APIs (API Keys)
Many commercial time/IP services (like IpStack, AbstractAPI, etc.) require an API key. You can provide it in two ways depending on the API requirements:
Option 1: Via Headers (Recommended)
If the service expects the key in a header (e.g., X-API-Key or Authorization), pass a pre-configured Guzzle client:
use Grebennik\IpClock\IpClock; use GuzzleHttp\Client; $client = new Client([ 'headers' => [ 'X-API-Key' => 'your_secret_key_here' ] ]); $clock = new IpClock(httpClient: $client, apiUrl: 'https://api.example.com/time');
Option 2: Via Query Parameters
If the key must be in the URL, use the {ip} placeholder to define where the IP address should be inserted:
use Grebennik\IpClock\IpClock; // The {ip} placeholder will be replaced with the actual IP address $apiUrl = 'https://api.example.com/time?key=your_secret_key&ip={ip}'; $clock = new IpClock( ip: '8.8.8.8', apiUrl: $apiUrl, parser: new MyCustomParser() );
Custom API and Response Parsing
If you use a different API that returns data in a different format, you can implement ResponseParserInterface and pass it to the constructor:
use Grebennik\IpClock\IpClock; use Grebennik\IpClock\ResponseParserInterface; use DateTimeImmutable; class MyCustomParser implements ResponseParserInterface { public function parse(array $data): DateTimeImmutable { // Your logic to extract time from $data return new DateTimeImmutable($data['custom_time_field']); } } $clock = new IpClock( apiUrl: 'https://my-custom-api.com/time', parser: new MyCustomParser() );
You can also inject your own HTTP client (Guzzle or any Psr\Http\Client\ClientInterface) via the constructor.
Testing
Run tests with:
vendor/bin/phpunit tests
License
MIT
grebennik/ip-clock 适用场景与选型建议
grebennik/ip-clock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 grebennik/ip-clock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grebennik/ip-clock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-24