lupuscoding/webhook-sender

用于webhook请求的简单发送器。

1.0.0 2021-12-15 23:40 UTC

This package is auto-updated.

Last update: 2024-09-24 19:40:46 UTC


README

用于webhook请求的简单发送器。

内容

需求 #

  • PHP >= 7.4

安装 #

composer require lupuscoding/webhook-sender

使用 #

向webhook发送消息

发送器接受实现了JsonSerializable接口的对象。只需初始化一个实现了该接口的对象,并将其传递给Sender::send方法。

use LupusCoding\Webhooks\Sender\Sender;
// Init your serializable object
/** @var JsonSerializable $mySerializableObject */
$mySerializableObject = new MySerializableObject();
// Setup the hook url
$webhookUrl = 'https://httpbin.org/post';
// Init sender
$sender = new Sender($webhookUrl, false);
// Send object ot webhook
$sender->send($mySerializableObject);
// Get response
$response = $sender->getLastResponse();

创建有效的可序列化对象

通过实现JsonSerializable接口并创建jsonSerialize方法,您可以决定哪些数据将被发送。

use JsonSerializable;

class MySerializableObject implements JsonSerializable
{
    private string $stringToPush;
    private string $stringToProcess;
    private bool $boolToPush;
    private bool $onlyToProcess;
    
    /* Getters and Setters may be here */
    
    public function jsonSerialize(): array
    {
        return [
            'stringToPush' => $this->stringToPush,
            'boolToPush' => $this->boolToPush,
        ];
    }
}

此示例有两个属性应推送/发送,以及两个属性不应发送。

开发 #

  • 每个贡献都应遵守PSR-2和PSR-12。
  • 方法必须提供参数类型和返回类型。
  • 类属性必须是类型化的。
  • doc blocks必须只包含描述性信息。
  • doc blocks可以额外包含参数或返回值的类型声明,如果类型声明不够精确。

例如:func(): array可能不够精确,如果方法返回数组的数组或对象。考虑使用doc block条目如@return array[]@return MyObject[]进行说明。

测试 #

Webhook测试站点: https://httpbin.org

首先通过执行以下命令安装phpunit

composer install

然后通过执行以下命令启动phpunit

vendor/bin/phpunit

可选:查看webhook测试站点以获取更多信息。