EDITED:
Since posting this answer MailChimp has released version 2 & 3 of their API. Version 3 will be the only supported version starting in 2017. As soon as I have a chance to test it, I will update this answer for API version 3.
MailChimp API v3.0
根据本页顶部的通知 https://apidocs.mailchimp.com/api/2.0/,2016 年之后将不再支持所有先前版本的 API。
我的解决方案在后台使用 PHP 来处理 API,并使用 jQuery 来促进 Ajax。
1) 下载支持 API v3.0 的 PHP 包装器。截至撰写本文时,最新的 MailChimp 文档中没有正式列出任何支持 v3.0 的内容,但 GitHub 上列出了一些,因此我选择this one https://github.com/drewm/mailchimp-api.
2)创建以下PHP文件,store-address.php
,使用您自己的 API 密钥和列表 ID,然后将其放在与第一步中的包装器相同的目录中。请记住遵循包装器的文档,但它们看起来都与此非常相似。
<?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:
(原答案)
摸索了一段时间后,我找到了一个使用 PHP 示例和 jQuery 的网站。由此,我能够使用 jQuery 创建一个简单的 HTML 页面,其中包含基本的注册表单。 PHP 文件“隐藏”在后台,用户永远看不到它们,但 jQuery 仍然可以访问和使用。
1) 在此处下载 PHP 5 jQuery 示例...(EDIT: 链接失效了。然而,唯一重要的部分是 PHP 的官方 API 包装器,它可用HERE https://bitbucket.org/mailchimp/mailchimp-api-php.)
http://apidocs.mailchimp.com/downloads/mcapi-simple-subscribe-jquery.zip http://apidocs.mailchimp.com/downloads/mcapi-simple-subscribe-jquery.zip
如果您只有 PHP 4,只需下载 MCAPI 1.2 版本并替换相应的MCAPI.class.php
上面的文件。
http://apidocs.mailchimp.com/downloads/mailchimp-api-class-1-2.zip 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>
所需件...
索引.html如上或类似构造。使用 jQuery,外观和选项是无穷无尽的。
商店地址.php文件作为 Mailchimp 站点上 PHP 示例的一部分下载,并使用您的API KEY and LIST ID。您需要将其他可选字段添加到数组中。
MCAPI.class.php从 Mailchimp 站点下载的文件(PHP 5 为 1.3 版,PHP 4 为 1.2 版)。将其放在与您的目录相同的目录中商店地址.php或者您必须更新其中的 url 路径商店地址.php所以它可以找到它。