noplanman/service-webhook-handler

处理来自各种服务的 Webhooks 的库。

1.0.0 2022-05-27 22:46 UTC

This package is auto-updated.

Last update: 2024-09-05 01:00:41 UTC


README

Minimum PHP Version Latest Stable Version Total Downloads License

PHP 库,用于处理来自各种服务的 Webhooks。

要求

通过 Composer 快速安装

此命令将使用 Guzzle HTTP 客户端快速启动。

composer require noplanman/service-webhook-handler:^1.0 guzzlehttp/guzzle:^7.4.3 guzzlehttp/psr7:^2.2

HTTP 客户端

您可以使用以下方式显式设置 HTTP 客户端:

use NPM\ServiceWebhookHandler\Utils;

// Example with Guzzle ($ composer require guzzlehttp/guzzle:^7.4.3 guzzlehttp/psr7:^2.2)
use GuzzleHttp\Client;
Utils::setClient(new Client());
use NPM\ServiceWebhookHandler\Utils;

// Example with HTTPlug with cURL ($ composer require php-http/curl-client:^2.0 nyholm/psr7:^1.1)
use Http\Client\Curl\Client;
Utils::setClient(new Client());

缓存

要启用缓存,请添加任何 PSR-16 兼容适配器 并设置缓存提供程序

use NPM\ServiceWebhookHandler\Utils;

// Example with redis ($ composer require cache/redis-adapter)
use Cache\Adapter\Redis\RedisCachePool;
$client = new Redis();
$client->connect('127.0.0.1', 6379);
Utils::setCache(new RedisCachePool($client));
use NPM\ServiceWebhookHandler\Utils;

// Example with memcached ($ composer require cache/memcached-adapter)
use Cache\Adapter\Memcached\MemcachedCachePool;
$client = new Memcached();
$client->addServer('localhost', 11211);
Utils::setCache(new MemcachedCachePool($client));

用法

到目前为止,为以下提供了一些基本功能:

GitHub

文档 - GitHubHandler.php

use NPM\ServiceWebhookHandler\Handlers\GitHubHandler;

$handler = new GitHubHandler('webhook_secret');
if ($handler->validate()) {
    // All good, use the received data!
    $data = $handler->getData();
}

GitLab

文档 - GitLabHandler.php

use NPM\ServiceWebhookHandler\Handlers\GitLabHandler;

$handler = new GitLabHandler('webhook_secret');
if ($handler->validate()) {
    // All good, use the received data!
    $data = $handler->getData();
}

Travis CI

文档 - TravisCIHandler.php

use NPM\ServiceWebhookHandler\Handlers\TravisCIHandler;

$handler = new TravisCIHandler();
if ($handler->validate()) {
    // All good, use the received data!
    $data = $handler->getData();
}

Telegram 登录

文档 - TelegramLoginHandler.php

use NPM\ServiceWebhookHandler\Handlers\TelegramLoginHandler;

$handler = new TelegramLoginHandler('123:BOT_API_KEY');
if ($handler->validate()) {
    // All good, use the received data!
    $data = $handler->getData();
}