Skip to content

Instantly share code, notes, and snippets.

@bwente
Created March 12, 2019 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwente/40e1fce30766466f53298907080135f6 to your computer and use it in GitHub Desktop.
Save bwente/40e1fce30766466f53298907080135f6 to your computer and use it in GitHub Desktop.
Write MODX FormIt submission to JSON
<?php
// config settings
$server = $hook->formit->config['server'];
$server = (!empty($server)) ? $server : 'https://www.modx.com';
$formname = $hook->formit->config['formname'];
$formname = (!empty($formname)) ? $formname : '';
$resultTpl = $hook->formit->config['resultTpl'];
$toPlaceholder = $hook->formit->config['toPlaceholder'];
// EXTRA settings
$today = date("m/d/Y");
$ip = $_SERVER['REMOTE_ADDR'];
$settings = array(
"ActionDate" => $today,
"IPAddress" => $ip
);
$formvalues = $hook->getValues();
$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_ERROR, '[JSON] ' . $json);
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