Mautic Community Forums

PHP submit Mautic form

Can anyone tell me what’s wrong with this PHP page that tries to submit a simple form Mautic?


The Code:

<?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)
{
//echo "data is $data";
//print_r(array_values ($data));

// 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;

// return has to be part of the form data array
if (!isset($data)) {
$data = 'http://www.myurl.com.br';
}

//$data = array('mauticform' => $data);
$data = array ( 'mauticform_getlead' => $data );

echo nl2br(" n ");
print_r(array_values ($data)); //**************************

// Change [path-to-mautic] to URL where your Mautic is
$formUrl = 'http://myurl.com.br/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);

if(curl_exec($ch) === false)
{
echo " | ";
echo 'Curl error: ' . curl_error($ch);
}


curl_close($ch);

return $response;
}


pushMauticForm(
array(
'mauticform_input_getlead_email' => "example@gmail.com",
),
75
);


?>
The Form:








E-Mail:

Ops, informe o e-mail



Submit









Can anyone tell me what’s wrong with this PHP page that tries to submit a simple form Mautic?

The Code:

<?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) { //echo "data is $data"; //print_r(array_values ($data)); // 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://www.myurl.com.br'; } //$data = array('mauticform' => $data); $data = array ( 'mauticform_getlead' => $data ); echo nl2br(" n "); print_r(array_values ($data)); //************************** // Change [path-to-mautic] to URL where your Mautic is $formUrl = 'http://myurl.com.br/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); if(curl_exec($ch) === false) { echo " | "; echo 'Curl error: ' . curl_error($ch); } curl_close($ch); return $response; } pushMauticForm( array( 'mauticform_input_getlead_email' => "example@gmail.com", ), 75 ); ?>

The Form:

        <div id="mauticform_getlead_email"  data-validate="email" data-validation-type="email" class="mauticform-row mauticform-email mauticform-required">
            <label id="mauticform_label_getlead_email" for="mauticform_input_getlead_email" class="mauticform-label">E-Mail:</label>
            <input id="mauticform_input_getlead_email" name="mauticform[email]" value="" class="mauticform-input" type="email" />
            <span class="mauticform-errormsg" style="display: none;">Ops, informe o e-mail</span>
        </div>

        <div id="mauticform_getlead_submit"  class="mauticform-row mauticform-button-wrapper">
            <button type="submit" name="mauticform[submit]" id="mauticform_input_getlead_submit" name="mauticform[submit]" value="" class="mauticform-button btn btn-default" value="1">Submit</button>
        </div>

        <input type="hidden" name="mauticform[formId]" id="mauticform_getlead_id" value="75" />
        <input type="hidden" name="mauticform[return]" id="mauticform_getlead_return" value="" />
        <input type="hidden" name="mauticform[formName]" id="mauticform_getlead_name" value="getlead" />

    </div>
</form>

Anothet test:

<?php function pushMauticForm2($data, $formId) { /** * Push data to a Mautic form * * @param array $data * @param integer $formId */ $data['formId'] = $formId; $data['return'] = 'http://www.myurl.com.br'; $data = array('mauticform' => $data); //$formUrl = $this->getMauticBaseUrl() . '/form/submit?formId=' . $formId; $formUrl = 'http://myurl.com.br/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)); // receive server response ... curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); } pushMauticForm2( array( '[b]mauticform[email][/b]' => "example@gmail.com", ), 75 ); ?>

Another example submiting an kiosk form to insert the contact (lead) on a segment. The Mautic reacts but does not record the action.

<?php //set POST variables $url = 'http://myurl.com.br/form/submit?formId=7'; $fields = array( 'mauticform[email]' => urlencode($_POST['example@gmail.com']), 'mauticform[formId]' => urlencode($_POST['75']), ); //url-ify the data for the POST foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string,'&'); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); //execute post $result = curl_exec($ch); //close connection curl_close($ch); ?>