Error in showing confirmation or error message

0

I have a problem sending the confirmation of the sending of the form, I am not showing any when completing the data or having a shipping problem. The link with Mailchimp if it works perfect.

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$cel = $_POST ['cel'];

if($email) {
    //Create mailchimp API url
    $memberId = md5(strtolower($email));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    print $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;

    //Member info
        $data = array(
            'email_address'=>$email,
            'status' => 'subscribed',
            'merge_fields'  => [
                'FNAME'     => $fname,
                'LNAME'     => $lname,
                'CEL'       => $cel
            ]
            );
    $jsonString = json_encode($data);

    // send a HTTP POST request with curl
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    //Collecting the status
    switch ($httpCode) {
        case 200:
            $msg = '¡Muchisimas gracias! Dentro de poco, nos comunicaremos con vos';
            break;
        case 214:
            $msg = 'Ya estas registrado en nuestra web';
            break;
        default:
            $msg = 'Oops, ocurrio un problema con el mensaje.[msg_code='.$httpCode.']';
            break;
    }
}

header('location:subscribe.php?msg='.$msg);

}

    
asked by Alex Milano 13.08.2018 в 16:25
source

1 answer

0

Solved! Here is the API link - > mailchimp.php

<?php

// Put your MailChimp API and List ID hehe
$api_key = 'MyAPIKEY';
$list_id = 'MyListID';

// Let's start by including the MailChimp API wrapper
include('./inc/MailChimp.php');
// Then call/use the class
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp($api_key);

// Submit subscriber data to MailChimp
// For parameters doc, refer to: http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/
// For wrapper's doc, visit: https://github.com/drewm/mailchimp-api
$result = $MailChimp->post("lists/$list_id/members", [
                        'email_address' => $_POST["email"],
                        'merge_fields'  => ['FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"], 'CEL'=>$_POST["cel"]],
                        'status'        => 'subscribed',
                    ]);

if ($MailChimp->success()) {
    // Success message
    echo "<h3>¡Muchisimas gracias! <br> Dentro de poco, nos pondremos en contacto con vos</h3>";
} else {
    // Display error
    echo $MailChimp->getLastError();
    // Alternatively you can use a generic error message like:
    echo "<h3>Por favor intentenlo de nuevo.</h3>";
}
?>
    
answered by 13.08.2018 в 18:54