Created
September 28, 2018 14:32
-
-
Save bwente/e6d8a01f385fdab51b0d27477b81e893 to your computer and use it in GitHub Desktop.
FormIt Post JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// config settings | |
$server = $hook->formit->config['server']; | |
$server = (!empty($server)) ? $server : 'http://www.apiwebhook.com/rest/'; | |
$formname = $hook->formit->config['formname']; | |
$resultTpl = $hook->formit->config['resultTpl']; | |
$toPlaceholder = $hook->formit->config['toPlaceholder']; | |
// from system settings | |
$username = $modx->getOption('webhook_username'); | |
$password = $modx->getOption('webhook_password'); | |
$settings = array( | |
"username" => $username, | |
"password" => $password | |
); | |
$formvalues = $hook->getValues(); | |
unset($formvalues[$submitVar]); | |
$data_string = array_merge($settings, $formvalues); | |
$json = json_encode($data_string); | |
$url = $server . $formname; | |
$headers = array( | |
'content-type:application/json', | |
'charset:utf-8', | |
'accept:application/json' | |
); | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_POST, true ); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt( $ch, CURLINFO_HEADER_OUT, true); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, "$json" ); | |
$result = curl_exec($ch); | |
$header = curl_getinfo($ch, CURLINFO_HEADER_OUT); | |
curl_close($ch); | |
$modx->log(modX::LOG_LEVEL_DEBUG, '[JSON] ' . $result); | |
$array = json_decode($result, true); | |
$modx->toPlaceholders($array); | |
$output = $modx->getChunk($resultTpl, $array); | |
if (!empty($toPlaceholder)) { | |
$modx->setPlaceholder($toPlaceholder, $output); | |
return ''; | |
} | |
return true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment