noplanman/service-webhook-handler

Library to handle Webhooks from various services.

1.0.0 2022-05-27 22:46 UTC

This package is auto-updated.

Last update: 2022-09-04 20:57:12 UTC


README

Minimum PHP Version Latest Stable Version Total Downloads License

PHP library to handle Webhooks from various services.

Requirements

Quick install via Composer

This command will get you up and running quickly with a Guzzle HTTP client.

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

HTTP Client

You can explicitly set an HTTP client with:

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());

Caching

To enable caching, add any PSR-16 compatible adapter and set the cache provider:

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));

Usage

Very basic functionality provided so far for:

GitHub

Docs - 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

Docs - 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

Docs - TravisCIHandler.php

use NPM\ServiceWebhookHandler\Handlers\TravisCIHandler;

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

Telegram Login

Docs - 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();
}