evoluted / webhooks
这个Laravel包可以帮助您组织、安全地将webhooks映射到事件。
Requires
- laravel/framework: ^5.3|^7.0|^8.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ~5.4
This package is auto-updated.
Last update: 2024-09-09 17:48:15 UTC
README
注意:这个仓库是 obrignoni/webhooks 的副本,该仓库最近被其作者删除。这个副本不在积极开发中。
这个Laravel包可以帮助您组织、安全地将webhooks映射到事件。
安装
要开始使用,请通过Composer包管理器安装Laravel Webhooks
composer require obrignoni/webhooks
接下来,在config/app.php配置文件的服务提供者数组中注册Webhooks服务提供者
Obrignoni\Webhooks\WebhooksServiceProvider::class,
为什么要使用webhooks包?
我想...
- 将我的应用程序与多个服务的webhooks集成。
- 保持webhooks的组织和安全。
- 将webhook事件映射到Laravel事件。
使用方法
以Github Webhooks为例。
Github webhook的有效负载看起来像这样...
POST /payload HTTP/1.1
Host: localhost:4567
X-Github-Delivery: 72d3162e-cc78-11e3-81ab-4c9367dc0958
User-Agent: GitHub-Hookshot/044aadd
Content-Type: application/json
Content-Length: 6615
X-GitHub-Event: issues
{
"action": "opened",
"issue": {
"url": "https://api.github.com/repos/octocat/Hello-World/issues/1347",
"number": 1347,
...
},
"repository" : {
"id": 1296269,
"full_name": "octocat/Hello-World",
"owner": {
"login": "octocat",
"id": 1,
...
},
...
},
"sender": {
"login": "octocat",
"id": 1,
...
}
}
注意,X-GitHub-Event
头部包含了issues
,这是Github Events之一。
我们可以使用Artisan命令来设置Github的webhook。
php artisan make:webhook github
这将创建App\Http\Webhooks\Github
类。
webhook类扩展了WebhookRequest,它也扩展了FormRequest,并添加了一些额外的配置选项。
让我们看一下生成的类。
<?php namespace App\Http\Webhooks; use Obrignoni\Webhooks\Http\WebhookRequest; class Github extends WebhookRequest { protected $eventField = ''; protected $events = []; protected $authorization = null; public function authorize() { // Authorize the webhook the same way you would with a FormRequest. return false; } public function rules() { return [ ]; } }
我们可以这样设置我们的Github webhook...
<?php namespace App\Http\Webhooks; use Obrignoni\Webhooks\Authorization\GithubAuthorization; use Obrignoni\Webhooks\Http\WebhookRequest; class Github extends WebhookRequest { protected $eventField = 'X-GitHub-Event'; protected $authorization = GithubAuthorization::class; }
事件字段
事件字段是包含事件值的请求参数或头部。
授权处理器
授权处理器可以包含授权请求的逻辑。它应该返回一个布尔值。
$authorization
被设置为这个包包含的GithubAuthorization
处理器。使用授权处理器是可选的,它可以用来替代authorize。
映射的事件
您使用$events
数组将每个webhook事件映射到事件类。如果为空,事件名称将被自动转换为Studly Cased类。对于Github webhook,pull_request
事件将被转换为App\Events\GithubPullRequest
。
以下是如何设置自定义事件的示例。
<?php namespace App\Http\Webhooks; use Obrignoni\Webhooks\Authorization\GithubAuthorization; use Obrignoni\Webhooks\Http\WebhookRequest; class Github extends WebhookRequest { protected $field = 'X-GitHub-Event'; protected $authorization = GithubAuthorization::class; protected $events = [ 'issue_comment' => 'App\Events\SomebodyMadeACommentOnGithub', 'pull_request' => 'App\Events\SomebodySubmittedAPullRequest', ]; }
Webhook请求扩展表单请求
您可以选择使用authorize
和rules
方法,就像Form Requests一样。