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