MailChimp API 2.0 and PHP Subscribe Form

点点圈 提交于 2019-12-20 06:28:10

问题


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. :(


回答1:


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 :)



来源:https://stackoverflow.com/questions/21395410/mailchimp-api-2-0-and-php-subscribe-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!