public function onBeforeDispatchWebhook(mixed &$payload, array &$headers, object $hook, VAPWebHook $job) : void

Fires before transmitting the payload to the web hook.


Description

Web hooks are used to immediately notify other websites when specific events trigger. In example, it is possible to post the appointment data to a different host every time a new reservation is created.

Plugins can use this trigger to manipulate the payload to post and the request headers.

By setting the payload to false, the dispatcher will automatically skip the web hook.

The system always includes the following headers by default:

  • Content-Type: application/json
  • X-VAP-WEBHOOK-ID: {ID} (the web hook identifier)
  • X-VAP-WEBHOOK-ACTION: {ACTION} (the action that triggered the web hook)
  • X-VAP-WEBHOOK-SECURE: {SECURE} (the web hook secret key, passed to MD5 algorithm)

Parameters

&$payload

(mixed)  The payload to send.

&$headers

(array)  An associative array containing the request headers.

$hook

(object)  An object holding the web hook details, such as the URL to notify and a secret key.

$job

(VAPWebHook)  The instance used to generate the payload.

Return Value

None.


Example

The following example explains how to implement your own BASIC HTTP authentication scheme.

/**
 * Plugins can use this event to manipulate the payload to post and the
 * request headers. By setting the payload to "false", the dispatcher
 * will automatically skip the web hook.
 *
 * @param   mixed       &$payload  The payload to send.
 * @param   array       &$headers  The request headers.
 * @param   object      $hook      The web hook details.
 * @param   VAPWebHook  $job       The web hook job.
 *
 * @return  void
 */
public function onBeforeDispatchWebhook(&$payload, &$headers, $hook, $job)
{
    // check whether we are contacting a specific end-point
    if (stripos($hook->url, 'mydomain.com'))
    {
        // include http basic authentication
        $headers['Authorization'] = 'Basic ' . base64_encode('{username}:{password}');
    }
}

Changelog

VersionDescription
1.7 Introduced.
Last Update: 2021-10-08 16:24
Helpful?