Search code examples
phpwordpressformsmailchimpmailchimp-api-v3.0

I want to integrate mailchimp with wordpress without using plugins


I have used this after my form.But my entry does not go to the mailchimp account. Please tell me if anything missing. Please refer the below code.

<?php

// SUBSCRIBE TO MAILING LIST OPTION - ADD TO MAILCHIMP USING API
if ($_POST['emailUpdates'] == 'Yes')
{
    // Include Mailchimp API class
    require_once('MCAPI.class.php');

    // Your API Key: http://admin.mailchimp.com/account/api/
    $api = new MCAPI('xxxxxxxxx-us15');

    // Your List Unique ID: http://admin.mailchimp.com/lists/ (Click "settings")
    $list_id = "xxxxxxx";

    // Variables in your form that match up to variables on your subscriber
    // list. You might have only a single 'name' field, no fields at all, or more
    // fields that you want to sync up.
    $merge_vars = array(
        'FNAME' => $_POST['signup_first_name'],
        'LNAME' => $_POST['signup_last_name']
    );

    // SUBSCRIBE TO LIST
    if ($api->listSubscribe($list_id, $_POST['signup_user_email'], $merge_vars) === true)
    {
        $mailchimp_result = 'Success! Check your email to confirm sign up.';
    }
    else
    {
        $mailchimp_result = 'Error: ' . $api->errorMessage;
    }
}

?>

Solution

  • integrate mailchimp form without using sdk class

     $email= $_POST["email"];
    
    $api_key = "dsfdfdsfdsdsfsfdgdfg-us15"; //api key
    $list_id = "2f35dfgdgffec5c2"; // list id
    
    
      $url = 'https://us15.api.mailchimp.com/2.0/lists/subscribe.json?apikey='.$api_key.'&id='.$list_id.'&email[email]='.$email.'&double_optin=false&send_welcome=false';
    echo callMailchimp($url);
    function callMailchimp($url)
    {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => 2
        ));
    
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    

    https://www.quora.com/How-do-I-use-PHP-cURL-to-access-the-new-MailChimp-API-v3-0