简体   繁体   English

MailChimp API 2.0和PHP订阅表单

[英]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. 我希望它进入MailChimp列表,我的设置中包含电子邮件,名字和姓氏。 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. 无论如何,在查看了文档之后(我不是一个超级专家),但是我将PHP放在一起,以便在提交表单后立即对其进行处理。 Anyways it doesn't work, the email doesn't get added to my MailChimp list. 无论如何,该电子邮件不会被添加到我的MailChimp列表中。 I even double checked the API Key and List ID. 我什至仔细检查了API密钥和列表ID。 Below is my code that has to do with the MailChimp subscription processing: 以下是与MailChimp订阅处理有关的代码:

<?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");" 现在是“ redirect_to(“ ./# subcribed”);“ gets called, but nothing is sent to the MailChimp list. 被调用,但没有任何内容发送到MailChimp列表。 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 :) 任何进一步的输入将是巨大的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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