cahuk/server-timezone
Composer 安装命令:
composer require cahuk/server-timezone
包简介
Compatible Clock that gets server time from IP-based geolocation
README 文档
README
A PHP package to resolve the current time based on an IP address using various external GeoIP and time APIs.
Installation
composer require cahuk/server-timezone
Running Unit Tests
Before running tests, clone the repository and install dependencies:
composer install
composer test
Usage Example
Below is a single comprehensive example demonstrating different approaches to resolve time based on IP:
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Psr\Http\Message\ResponseInterface; use Cahuk\ServerTimezone\Core\Services\TimeResolverService; use Cahuk\ServerTimezone\Infrastructure\TimeProvider\TimeResolverProvider; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\GeoIpTimeApiResolver; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\HttpClient\HttpConfig; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\IpFetcher\Api\IpApiFetcher; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\Services\TimeApiFetcherService; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\TimeFetcher\Api\HttpTimeFetcher; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\IpFetcher\Api\HttpIpFetcherResponse; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\IpFetcher\Api\Mappers\IpifyOrgIpMapper; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\TimeFetcher\Api\Mappers\TimeapiIoTimeMapper; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\IpFetcher\Api\HttpIpFetcherResponseInterface; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\IpFetcher\Api\Client\GuzzleHttpIpFetcherClient; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\TimeFetcher\Api\Mappers\IpgeolocationIoTimeMapper; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\TimeFetcher\Api\Client\GuzzleHttpTimeFetcherClient; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\IpFetcher\Api\HttpIpFetcherResponseMapperInterface; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\Services\TimeParametersMappers\DefaultMappingFields; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\Services\TimeParametersMappers\DefaultTimeParametersMappers; use Cahuk\ServerTimezone\Infrastructure\TimeResolver\GeoIpTimeResolver\Services\TimeParametersMappers\DefaultMappingTimeParameters; // ------------------------------------------------------------ // Example 1: Fetch current time for a known external IP address $timeServerHost1 = 'https://timeapi.io'; $timeServerUrl1 = '/api/time/current/ip'; $timeServerHeaders1 = ['Accept' => 'application/json']; $timeServerParams1 = ['ipAddress' => '89.151.31.13']; $apiTimeProvider1 = new TimeResolverProvider( new GeoIpTimeApiResolver( new HttpTimeFetcher( new GuzzleHttpTimeFetcherClient( new HttpConfig( $timeServerHost1, $timeServerUrl1, headers: $timeServerHeaders1, parameters: $timeServerParams1, ), new IpgeolocationIoTimeMapper() ) ) ) ); var_dump((new TimeResolverService($apiTimeProvider1))->resolveTime()->now()); // ------------------------------------------------------------ // Example 2: Use separate services to fetch IP and then time by IP $ipServerHost2 = 'https://api.ipify.org'; $ipServerUrl2 = ''; $timeServerHost2 = 'https://timeapi.io'; $timeServerUrl2 = '/api/time/current/ip'; $timeServerHeaders2 = ['Accept' => 'application/json']; $apiTimeProvider2 = new TimeResolverProvider( new GeoIpTimeApiResolver( new TimeApiFetcherService( new IpApiFetcher( new GuzzleHttpIpFetcherClient( new HttpConfig($ipServerHost2, $ipServerUrl2), new IpifyOrgIpMapper() ) ), new GuzzleHttpTimeFetcherClient( new HttpConfig( $timeServerHost2, $timeServerUrl2, headers: $timeServerHeaders2 ), new TimeapiIoTimeMapper() ), new DefaultTimeParametersMappers([ DefaultMappingFields::IPV4->value => 'ipAddress' ]), new DefaultMappingTimeParameters() ) ) ); var_dump((new TimeResolverService($apiTimeProvider2))->resolveTime()->now()); // ------------------------------------------------------------ // Example 3: Inline anonymous mapper for IP fetch response $ipServerHost3 = 'https://ifconfig.me'; $ipServerHeaders3 = [ 'User-Agent' => 'curl/7.64.1', 'Accept' => 'text/plain', ]; $timeServerHost3 = 'https://timeapi.io'; $timeServerUrl3 = '/api/time/current/ip'; $timeServerHeaders3 = ['Accept' => 'application/json']; $apiTimeProvider3 = new TimeResolverProvider( new GeoIpTimeApiResolver( new TimeApiFetcherService( new IpApiFetcher( new GuzzleHttpIpFetcherClient( new HttpConfig($ipServerHost3, headers: $ipServerHeaders3), new class implements HttpIpFetcherResponseMapperInterface { #[\Override] public function map(ResponseInterface $response): HttpIpFetcherResponseInterface { $body = $response->getBody(); $responseData = (string)$body; return new HttpIpFetcherResponse($responseData); } } ) ), new GuzzleHttpTimeFetcherClient( new HttpConfig( $timeServerHost3, $timeServerUrl3, headers: $timeServerHeaders3 ), new TimeapiIoTimeMapper() ), new DefaultTimeParametersMappers([ DefaultMappingFields::IPV4->value => 'ipAddress' ]), new DefaultMappingTimeParameters() ) ) ); var_dump((new TimeResolverService($apiTimeProvider3))->resolveTime()->now()); // ------------------------------------------------------------ // Example 4: Direct time fetch from IP-based service (premium key required) $timeServerHost4 = 'https://api.ipgeolocation.io'; $timeServerUrl4 = '/v2/ipgeo'; $timeServerParams4 = ['apiKey' => 'YOUR_API_KEY_HERE']; // Replace with your premium API key $apiTimeProvider4 = new TimeResolverProvider( new GeoIpTimeApiResolver( new HttpTimeFetcher( new GuzzleHttpTimeFetcherClient( new HttpConfig( $timeServerHost4, $timeServerUrl4, parameters: $timeServerParams4 ), new IpgeolocationIoTimeMapper() ) ) ) ); // Uncomment to test with valid API key: // var_dump((new TimeResolverService($apiTimeProvider4))->resolveTime()->now());
Notes
- Replace YOUR_API_KEY_HERE in Example 4 with your valid premium API key.
- The examples demonstrate flexibility in using different IP and time services.
- You can extend or customize mappers to fit different API response formats.
Running the example script
php ./examples/example.php
cahuk/server-timezone 适用场景与选型建议
cahuk/server-timezone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cahuk/server-timezone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cahuk/server-timezone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-29