juststeveking / webhooks
开始使用PHP发送webhooks的最简单方式。
0.0.2
2023-03-19 19:33 UTC
Requires
- php: ^8.2
- php-http/discovery: ^1.15.2
- thecodingmachine/safe: ^2.4
Requires (Dev)
- laravel/pint: ^1.6
- nyholm/psr7: ^1.5.1
- pestphp/pest: ^1.22.6
- php-http/mock-client: ^1.5
- phpstan/phpstan: ^1.10.7
- symfony/http-client: ^6.2
- thecodingmachine/phpstan-safe-rule: ^1.2
Suggests
- nyholm/psr7: A fantastic PSR-7 library that also supports PSR-17
- symfony/http-client: A PSR-18 compliant HTTP Client that will work out of the box.
README
开始使用PHP发送webhooks的最简单方式。
安装
您可以使用composer安装此库
composer require juststeveking/webhooks
下一步是安装一个符合OSR规范的包来进行请求,您需要
- PSR 7 请求库
- PSR17 请求和流工厂
- PSR18 Http 客户端
此包会自动发现您已安装的这些包,因此您无需进行任何配置。
用法
此包非常简单易用,您只需构建一个类即可开始发送webhooks。
use JustSteveKing\Webhooks\Webhook; class YourWebhook extends Webhook { public function headers(): array { $signature = hash_hmac( algo: 'sha256', data: \Safe\json_encode( value: $this->buildPayload(), flags: JSON_THROW_ON_ERROR, ), key: 'Your signing key goes here', ); return [ 'Content-Type' => 'application/json', 'Signature' => $signature, ]; } public function buildPayload(): array { return [ 'foo' => 'bar', ]; } public function url(): string { return 'Your URL goes here.'; } public function method(): Method { return Method::POST; } public function plugins(): array { return []; } }
用于发送HTTP请求的HTTP客户端支持插件,您可以在这里了解它们是如何工作的(查看这里)。
测试
有一个可用的composer脚本来运行测试
composer test
但是,如果您无法运行此脚本,请使用以下命令
./vendor/bin/pest
安全
如果您发现任何与安全相关的问题,请通过电子邮件juststevemcd@gmail.com 而不是使用问题跟踪器来报告。