Skip to content

Instantly share code, notes, and snippets.

@escopecz
Last active March 4, 2022 07:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save escopecz/989cfc36fd2550304aa9 to your computer and use it in GitHub Desktop.
Save escopecz/989cfc36fd2550304aa9 to your computer and use it in GitHub Desktop.
<?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;
}
Copy link

ghost commented Feb 15, 2020

@escopecz Thanks For the snippet, im wondering if it's posible to include a tags into the fields array to be sent with the contact or i will have to use API to be able to do so?

@wgicio
Copy link

wgicio commented Mar 4, 2022

This script is CRAP! it works but don't expect to use it with high traffic, takes fucking 5-10 seconds for each submit. SOMOME needs to audit this code, because he doesn't know what he is DOING!!!!

write something yourself farktard

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