Last active
March 4, 2022 07:49
-
-
Save escopecz/989cfc36fd2550304aa9 to your computer and use it in GitHub Desktop.
Gist for article https://medium.com/@jan_linhart/the-simplest-way-how-to-submit-a-form-data-to-mautic-1454d3afd005#.2ggxlqsge
This file contains 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 | |
/** | |
* Push data to a Mautic form | |
* | |
* @param array $data The data submitted by your form | |
* @param integer $formId Mautic Form ID | |
* @param string $ip IP address of the lead | |
* @return boolean | |
*/ | |
function pushMauticForm($data, $formId, $ip = null) | |
{ | |
// Get IP from $_SERVER | |
if (!$ip) { | |
$ipHolders = array( | |
'HTTP_CLIENT_IP', | |
'HTTP_X_FORWARDED_FOR', | |
'HTTP_X_FORWARDED', | |
'HTTP_X_CLUSTER_CLIENT_IP', | |
'HTTP_FORWARDED_FOR', | |
'HTTP_FORWARDED', | |
'REMOTE_ADDR' | |
); | |
foreach ($ipHolders as $key) { | |
if (!empty($_SERVER[$key])) { | |
$ip = $_SERVER[$key]; | |
if (strpos($ip, ',') !== false) { | |
// Multiple IPs are present so use the last IP which should be the most reliable IP that last connected to the proxy | |
$ips = explode(',', $ip); | |
array_walk($ips, create_function('&$val', '$val = trim($val);')); | |
$ip = end($ips); | |
} | |
$ip = trim($ip); | |
break; | |
} | |
} | |
} | |
$data['formId'] = $formId; | |
// return has to be part of the form data array | |
if (!isset($data['return'])) { | |
$data['return'] = 'http://where-to-redirect.com'; | |
} | |
$data = array('mauticform' => $data); | |
// Change [path-to-mautic] to URL where your Mautic is | |
$formUrl = 'http://[path-to-mautic]/form/submit?formId=' . $formId; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $formUrl); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Forwarded-For: $ip")); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
write something yourself farktard