编辑:
自发布此答案以来,MailChimp 已发布其 API 的第 2 版和第 3 版。从 2017 年开始,版本 3 将是唯一受支持的版本。一旦我有机会对其进行测试,我将针对 API 版本 3 更新此答案。
MailChimp API v3.0
根据本页顶部的通知, 2016 年之后将不再支持所有先前版本的 API。
我的解决方案在后台使用 PHP 来处理 API,并使用 jQuery 来促进 Ajax。
1) 下载一个支持 API v3.0 的 PHP 包装器。在撰写本文时,最新的 MailChimp 文档中没有任何官方列出支持 v3.0,但在 GitHub 上列出了几个,所以我选择了这个。
2) 使用您自己的 API 密钥和列表 ID 创建以下 PHP 文件,store-address.php
然后将其放在与步骤一中的包装器相同的目录中。请记住遵循包装器的文档,但它们看起来都与此非常相似。
<?php // for MailChimp API v3.0
include('MailChimp.php'); // path to API wrapper downloaded from GitHub
use \DrewM\MailChimp\MailChimp;
function storeAddress() {
$key = "xxxxxxxxxxxxxxx-us1";
$list_id = "xxxxxx";
$merge_vars = array(
'FNAME' => $_POST['fname'],
'LNAME' => $_POST['lname']
);
$mc = new MailChimp($key);
// add the email to your list
$result = $mc->post('/lists/'.$list_id.'/members', array(
'email_address' => $_POST['email'],
'merge_fields' => $merge_vars,
'status' => 'pending' // double opt-in
// 'status' => 'subscribed' // single opt-in
)
);
return json_encode($result);
}
// If being called via ajax, run the function, else fail
if ($_POST['ajax']) {
echo storeAddress(); // send the response back through Ajax
} else {
echo 'Method not allowed - please ensure JavaScript is enabled in this browser';
}
3) 创建您的 HTML/CSS/JavaScript(jQuery) 表单(不需要在 PHP 页面上,访问者永远不会看到 PHP 正在后台使用。)
响应采用 JSON 格式,因此您必须正确处理它。
这是我的index.html
文件的样子:
<form id="signup" action="index.html" method="get">
First Name: <input type="text" name="fname" id="fname" />
Last Name: <input type="text" name="lname" id="lname" />
email Address (required): <input type="email" name="email" id="email" />
<input type="submit" id="SendButton" name="submit" value="Submit" />
</form>
<div id="message"></div>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#signup').submit(function() {
$("#message").html("Adding your email address...");
$.ajax({
url: 'inc/store-address.php', // proper url to your "store-address.php" file
type: 'POST', // <- IMPORTANT
data: $('#signup').serialize() + '&ajax=true',
success: function(msg) {
var message = $.parseJSON(msg),
result = '';
if (message.status === 'pending') { // success
result = 'Success! Please click the confirmation link that will be emailed to you shortly.';
} else { // error
result = 'Error: ' + message.detail;
}
$('#message').html(result); // display the message
}
});
return false;
});
});
</script>
MailChimp API 版本 1:
(原答案)
在摸索了一会儿之后,我找到了一个使用 jQuery 的 PHP 示例的站点。由此我能够使用包含基本注册表单的 jQuery 创建一个简单的 HTML 页面。PHP 文件被“隐藏”在后台,用户永远看不到它们,但 jQuery 仍然可以访问和使用。
1)在此处下载 PHP 5 jQuery 示例...(编辑:链接已失效。但是,唯一重要的部分是 PHP 的官方 API 包装器,可在此处获得。)
http://apidocs.mailchimp.com/downloads/mcapi-simple-subscribe-jquery.zip
如果您只有 PHP 4,只需下载 1.2 版的 MCAPI 并替换MCAPI.class.php
上面的相应文件。
http://apidocs.mailchimp.com/downloads/mailchimp-api-class-1-2.zip
2) 按照自述文件中的说明,将您的 API 密钥和列表 ID 添加到store-address.php
文件的适当位置。
3) 您可能还想收集用户的姓名和/或其他信息。store-address.php
您必须使用相应的合并变量将数组添加到文件中。
这是我的store-address.php
文件的样子,其中我还收集了名字、姓氏和电子邮件类型:
<?php
function storeAddress() {
require_once('MCAPI.class.php'); // same directory as store-address.php
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('123456789-us2');
$merge_vars = Array(
'EMAIL' => $_GET['email'],
'FNAME' => $_GET['fname'],
'LNAME' => $_GET['lname']
);
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "123456a";
if ($api->listSubscribe($list_id, $_GET['email'], $merge_vars , $_GET['emailtype'])) {
// It worked!
return 'Success! Check your inbox or spam folder for a message containing a confirmation link.';
} else {
// An error ocurred, return error message
return '<b>Error:</b> ' . $api->errorMessage;
}
}
// If being called via ajax, autorun the function
if($_GET['ajax']) {
echo storeAddress();
}
4) 创建您的 HTML/CSS/jQuery 表单。它不需要在 PHP 页面上。
这是我的index.html
文件的样子:
<form id="signup" action="index.html" method="get">
First Name: <input type="text" name="fname" id="fname" />
Last Name: <input type="text" name="lname" id="lname" />
email Address (required): <input type="email" name="email" id="email" />
HTML: <input type="radio" name="emailtype" value="html" checked="checked" />
Text: <input type="radio" name="emailtype" value="text" />
<input type="submit" id="SendButton" name="submit" value="Submit" />
</form>
<div id="message"></div>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#signup').submit(function() {
$("#message").html("Adding your email address...");
$.ajax({
url: 'inc/store-address.php', // proper url to your "store-address.php" file
data: $('#signup').serialize() + '&ajax=true',
success: function(msg) {
$('#message').html(msg);
}
});
return false;
});
});
</script>
所需件...
index.html构造如上或类似。使用 jQuery,外观和选项是无穷无尽的。
store-address.php文件作为 PHP 示例的一部分下载到 Mailchimp 站点上,并使用您的API KEY和LIST ID进行了修改。您需要将其他可选字段添加到数组中。
从 Mailchimp 站点下载的MCAPI.class.php文件(PHP 5 的 1.3 版或 PHP 4 的 1.2 版)。将它放在与您的store-address.php相同的目录中,或者您必须更新store-address.php中的 url 路径以便它可以找到它。