Skip to content

Instantly share code, notes, and snippets.

@bwente
Created November 8, 2017 15:55
Show Gist options
  • Save bwente/ad99e966f339cdba9bb88cbc3f3cb495 to your computer and use it in GitHub Desktop.
Save bwente/ad99e966f339cdba9bb88cbc3f3cb495 to your computer and use it in GitHub Desktop.
Very basic webhook for MODX, posts JSON to a URL. Works well with Zapier.
<?php
// [[!WebHook? &secret=`dh730>soteW` &runsnippet=`updateSettings` &params=`setting=savings`]]
$mySecret = $modx->getOption('secret', $scriptProperties);
$mySnippet = $modx->getOption('runsnippet', $scriptProperties);
$myParams = $modx->getOption('params', $scriptProperties);
$achunks = array_chunk(preg_split('/(=|,)/', $myParams), 2);
$myParams = array_combine(array_column($achunks, 0), array_column($achunks, 1));
$webhookContent = "";
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
$newJSON = $modx->fromJSON($webhookContent);
$allParams = array();
if(is_array($myParams))
$allParams = array_merge($allParams, $myParams);
$allParams = array_merge($allParams, $newJSON);
if ($newJSON['secret'] == $mySecret) {
$modx->log(modX::LOG_LEVEL_ERROR, '[WebHook] ' . $webhookContent);
$output = $modx->runSnippet($mySnippet, $allParams);
}
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment