loilo / github-webhook-handler
验证和利用GitHub webhook
1.0.1
2020-06-08 13:12 UTC
Requires
- php: ^7.2
- ext-hash: *
- ext-json: *
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.4
- mockery/mockery: ^1.3
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-09-24 07:08:00 UTC
README
GitHub Webhook Handler
使用PHP处理GitHub webhook。
安装
composer require loilo/github-webhook-handler
使用方法
首先创建一个处理器
$handler = new Loilo\GithubWebhook\Handler($secret);
$secret
是在GitHub后端定义的webhook密钥。它 可以 被省略,然而提供它 强烈推荐 以防止端点被滥用。
要处理一个请求(特别是,一个 PSR-7 ServerRequestInterface
),将那个请求传递给 handle
方法
$delivery = $handler->handle($request);
如果webhook请求无效,将抛出一个 特定失败的异常。否则,该方法返回一个 Delivery
对象,该对象提供对引起hook请求的事件及其随附的负载数据的访问
// Get the triggered event (e.g. 'push') $delivery->event(); // Get the whole webhook payload as an associative array $delivery->payload(); // Get a nested path in the payload (returns `null` if the path doesn't exist) $delivery->payload('repository.full_name');
单文件端点
可能有一些非常简单的情况,你不需要PHP框架来编写脚本,只想放置一个单一的PHP文件端点。为了避免处理异常和创建适当的错误响应,你可以使用 respond()
方法
$delivery = $handler->respond($request);
现在你可以安全地使用 $delivery
对象,无需担心更多——如果webhook请求的验证失败,处理器将设置适当的HTTP状态码,打印错误消息并 停止脚本执行。
提示:没有PSR-7请求对象?安装
guzzlehttp/psr7
包,并使用\GuzzleHttp\Psr7\ServerRequest::fromGlobals()
从环境中创建请求对象。