Skip to content

Instantly share code, notes, and snippets.

@escopecz
Last active March 4, 2022 07:49
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
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;
}
@keithmautic
Copy link

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!!!!

@escopecz
Copy link
Author

escopecz commented Apr 26, 2017

@keithmautic it's nice of you that you provided professional expertise of the script. The only thing that can take time is the CURL request to Mautic itself. How would you suggest to optimise that?

For others who doesn't fear to use the code from someone who "doesn't know what he's DOING!!!!", I built a PHP library which is designed to be even slower and also infects your dog with ear infection:

https://github.com/escopecz/mautic-form-submit

But seriously, it works better with the newer Mautic versions which use the cookie tracking instead of IP tracking. It can be easily installed via Composer to your project. Can't wait for more hateful comments celebrating my work!

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