Skip to content

Instantly share code, notes, and snippets.

@escopecz
Last active October 29, 2021 12:57
Show Gist options
  • Save escopecz/0d9adba753300b88326d to your computer and use it in GitHub Desktop.
Save escopecz/0d9adba753300b88326d to your computer and use it in GitHub Desktop.
A simple script to log a Mautic webhook request
<?php
/**
* A helper class to log and get the Mautic webhook request
*/
class webhookTest {
/**
* Log a message to a file
*
* @param mixed $message
* @param string $type [info|error|request]
*/
public function log($message, $type = 'info')
{
$prefix = 'webhookLog_';
$file = $type . '.log';
$date = new DateTime();
error_log($date->format('Y-m-d H:i:s') . ' ' . $message . "\n\n", 3, $prefix . $file);
}
/**
* Get the request JSON object and log the request
*
* @return object
*/
public function getRequest()
{
$rawData = file_get_contents("php://input");
$headers = apache_request_headers();
$this->log(print_r($headers, true), 'headers');
$this->log($rawData, 'request');
return json_decode($rawData);
}
}
$webhook = new webhookTest;
$requestData = $webhook->getRequest();
// @todo Process the $requestData as needed
@escopecz
Copy link
Author

This script is meant to ensure that the webhook is working and as a starting point to handle the webhook's payload. It will log every request to a log file so you know what is going on.

How to use it

  1. Upload the script above into some publicly accessible location. For example to http://example.com/webhookTest.php
  2. Create a Mautic webhook and point it to the URL from 1.
  3. Hit the Send Test Payload button in the webhook's edit form.
  4. Look at the folder where the webhookTest.php script was uploaded in 1. There should be a new file called webhookLog_request.php with the request time and data.

@IADDIC
Copy link

IADDIC commented Feb 15, 2017

@escopecz do you know how to send the result to goole sheets? I would like to capture data in a google sheet but I am unfamiliar with how that is accomplished.

Thanks, Rich

@BillPackard
Copy link

When go to Create new webhook and write webhook POST Url this one: https://---mautic-dir------/webhookTest.php after press Send Test Payload have a red error message: Error Detected.

@escopecz
Copy link
Author

Since this script I learned there are simpler ways how to test webhooks. Google for "request bin" or "post bin" to find the right mutation of the tool.

@fungayimakoni
Copy link

Wow been struggling with Kajabi webhook payload debugging until I found this. Works like a charm thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment