lupuscoding/webhook-sender

Simple Sender for webhook requests.

1.0.0 2021-12-15 23:40 UTC

This package is auto-updated.

Last update: 2024-05-24 19:02:41 UTC


README

Simple Sender for webhook requests.

Contents

Requirements #

  • PHP >= 7.4

Install #

composer require lupuscoding/webhook-sender

Usage #

Send a message to a webhook

The sender accepts objects, that implement the JsonSerializable interface. Just initialize an object, that implements the interface and hand it over, to the Sender::send method.

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

Create a valid serializable object

By implementing the JsonSerializable interface and creating the jsonSerialize method, you are able to decide, which data will be sent.

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,
        ];
    }
}

This example has two properties that should be pushed / send and two properties that should not be sent.

Development #

  • Every contribution should respect PSR-2 and PSR-12.
  • Methods must provide argument types and return types.
  • Class properties must be typed.
  • doc blocks must only contain descriptive information.
  • doc blocks may additionally contain a type declaration for arguments or return values, if the type declaration is not precise.

For example: func(): array may not be precise if the method returns an array of arrays or objects. Consider a doc block entry like @return array[] or @return MyObject[] for clarification.

Testing #

Webhook test site: https://httpbin.org

First install phpunit by executing

composer install

Then start phpunit by executing

vendor/bin/phpunit

Optional: Look at the webhook test site, to get more information.