[英]Mailchimp API - Subscribe to list
I'm working on adding a script to my site for a MailChimp subscribe form. 我正在为我的网站添加MailChimp订阅表单的脚本。 I think I have everything setup right but when I hit the subscribe button I'm getting a blank page.
我认为我的所有设置都正确,但是当我按下“订阅”按钮时,我得到的是空白页面。
Here is the script I have currently 这是我目前拥有的脚本
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once 'Mailchimp.php';
$apikey = "XXXXXXXXXXXXXXX";
$Mailchimp = new Mailchimp($apikey);
if (!empty($_POST)) {
$id = "XXXXXXXXXX";
$email = array(
'email' => trim($_POST['email'])
);
$result = $Mailchimp->$lists->subscribe($id, $email, $double_optin=false, $replace_interests=false);
var_dump($result);
}
echo "TESTING";
So I'm not getting the $result variable or "TESTING echo'd right now, so I assume I must be doing something simple wrong. Anyone see anything obvious? I believe I'm using the correct default JSON format. (keys have been X'd out, but the one's I'm using are correct) 因此,我现在没有得到$ result变量或“ TESTING echo”,所以我认为我必须做一些简单的错误。任何人都可以看到明显的东西?我相信我使用的是正确的默认JSON格式。(键具有被X淘汰,但我使用的是正确的)
Any help is much appreciated. 任何帮助深表感谢。
Thanks!! 谢谢!!
EDIT: I have updated the code to something I believe to be more correct, but it still isn't working. 编辑:我已经将代码更新为我认为更正确的内容,但仍然无法正常工作。 I could really use some help on this.
我真的可以为此提供一些帮助。
Here's how I've handled AJAX email submissions in the past for MailChimp's API (MCAPI): 这是我过去为MailChimp的API(MCAPI)处理AJAX电子邮件提交的方式:
define("MC_API_KEY", "Your mailchimp API key");
define("MC_API_LIST", "The list ID to subscribe user to");
define("EMAIL_TO", "email address in case subscription fails");
require "MailChimp.API.class.php";
function json($error = true, $message = "Unknown error") {
die(json_encode(array("error" => $error, "message" => $message)));
}
if(!empty($_POST)) {
$email = !empty($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ? $_POST['email'] : false;
if($email !== false) {
$api = new MCAPI(MC_API_KEY);
$result = $api->listSubscribe(MC_API_LIST, $email);
if($api->errorCode || !$result) {
if(isset($api->errorCode) && $api->errorCode == 214) { // already subscribed
json(true, "You are already subscribed!");
} else {
$error = "Unable to save user email via MailChimp API!\n\tCode=".$api->errorCode."\n\tMsg=".$api->errorMessage."\n\n";
$headers = "From: your@email.com\r\nReply-to: <{$email}>\r\nX-Mailer: PHP/".phpversion();
mail(EMAIL_TO, "Newsletter Submission FAIL [MC API ERROR]", "{$error}Saved info:\nFrom: {$email}\n\nSent from {$_SERVER['REMOTE_ADDR']} on ".date("F jS, Y \@ g:iA e"), $headers);
json(false, "Thank you - your email will be subscribed shortly!");
}
} else {
json(false, "Thanks - A confirmation link has been sent to your email!");
}
} else {
json(true, "Please enter your valid email address");
}
} else json();
SOLVED: 解决了:
Here is the correct code - hopefully this will help others looking to use the new api - 这是正确的代码-希望这会帮助希望使用新api的其他人-
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once 'Mailchimp.php';
$apikey = "XXXXXXXXXXXXXXXXXXX";
$Mailchimp = new Mailchimp($apikey);
if (!empty($_POST)) {
$id = "XXXXXXXXXX";
$email = array(
'email' => trim($_POST['email'])
);
$result = $Mailchimp->lists->subscribe($id, $email, $merge_vars=null, $double_optin=false, $replace_interests=false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.