简体   繁体   中英

MailChimp API 2.0 and PHP Subscribe Form

So I have a form that submits an email string. I want this to go into a MailChimp list I have setup that has an email, first name, and last name. For this particular form, I only want it to send an email and leave the first and last name fields blank. Anyways, after looking at the documentation (I'm not a super pro at this), but I put together the PHP that processes once the form has been submitted. Anyways it doesn't work, the email doesn't get added to my MailChimp list. I even double checked the API Key and List ID. Below is my code that has to do with the MailChimp subscription processing:

<?php
require_once("../includes/mailchimp/Mailchimp.php");

if (isset ($_POST['submitted'])) {
    $email = $database->escape_value(trim($_POST['email']));

     if (!empty ($email) && filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email)) {
        $session->save_email($email);
        // Subscribe User to List
        $api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        $list_id = "xxxxxxxxxxx";

        $Mailchimp = new Mailchimp( $api_key );
        $Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );
        $subscriber = $Mailchimp_Lists->subscribe( $list_id, array( 'email' => $email ) );

        if ( ! empty( $subscriber['leid'] ) ) {
            redirect_to("./#subcribed");
        }
        // end subscribe

    } else {
        redirect_to("./#hello-" . $email);
    }
}
?>

Now the "redirect_to("./#subcribed");" gets called, but nothing is sent to the MailChimp list. Any ideas on why it might not be working?

**EDIT I removed this:

$Mailchimp_Lists = new Mailchimp_Lists( $Mailchimp );

And then changed the subscribe function to:

$subscriber = $Mailchimp->lists->subscribe($list_id, array('email' => $email));

Anyways, It still does the same thing as before. :(

Figured it out! I needed to use the call method instead of the subscribe method. I'm really not sure why though. Any further input into this would be great :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM