MailChimp API 2.0和PHP订阅表单 [英] MailChimp API 2.0 and PHP Subscribe Form
问题描述
所以我有一个提交电子邮件字符串的表单。我想要这个进入一个MailChimp列表我有一个电子邮件,名字和姓氏的设置。对于这种特定的形式,我只希望它发送一封电子邮件,并将第一个和最后一个姓名字段留空。无论如何,在查看文档之后(我不是一个超级专业人士),但是,一旦提交了表单,我就把PHP处理。无论如何,电子邮件不会添加到我的MailChimp列表中。我甚至仔细检查了API密钥和列表ID。以下是我与MailChimp订阅处理有关的代码:
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);
}
}
?>
现在redirect_to(./# subcribed);被调用,但没有发送到MailChimp列表。任何关于为什么它可能不起作用的想法?
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 :)
这篇关于MailChimp API 2.0和PHP订阅表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!