承接 grebennik/ip-clock 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固