编辑:
自发布此答案以来,MailChimp已发布了其API的版本2和3。
从2017年开始,版本3将成为唯一受支持的版本。一旦有机会对其进行测试,我将针对API版本3更新此答案。
MailChimp API v3.0
根据此页面顶部的通知,
2016年之后将不再支持该API的所有早期版本。
我的解决方案在后台使用PHP处理API,并在jQuery中使用Ajax。
1)下载支持API
v3.0的PHP包装器。在撰写本文时,最新的支持v3.0的MailChimp文档中没有列出任何官方文档,但是GitHub上列出了一些文档,因此我选择了此文档。
2)
store-address.php使用您自己的API密钥和列表ID
创建以下PHP文件,然后将其放置在与第一步中的包装器相同的目录中。记住要遵循包装程序的文档,但是它们看上去都与此相似。
<?php // for MailChimp API v3.0include('MailChimp.php'); // path to API wrapper downloaded from GitHubuse DrewMMailChimpMailChimp;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_enpre($result);}// If being called via ajax, run the function, else failif ($_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示例…( 编辑
:链接已失效。但是,唯一重要的部分是PHP的官方API包装器,可在此处使用。)
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
2)按照自述文件中的指示,
store-address.php在适当的位置将API密钥和列表ID添加到文件中。
3)您可能还希望收集您的用户名和/或其他信息。您必须
store-address.php使用相应的合并变量将数组添加到文件中。
这是我的
store-address.php文件的样子,在这里我还收集了名字,姓氏和电子邮件类型:
<?phpfunction 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 messagereturn '<b>Error:</b> ' . $api->errorMessage; }}// If being called via ajax, autorun the functionif($_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,外观和选项无穷无尽。
*在Mailchimp网站上作为PHP示例的一部分下载了 *store-address.php 文件,并使用您的 API KEY 和 LIST ID进行了修改 。您需要将其他可选字段添加到数组。
*从Mailchimp站点下载的 *MCAPI.class.php 文件(PHP 5版本1.3或PHP 4版本1.2)。将其放置在与 store-address.php 相同的目录中,或者必须更新 store-address.php中 的url路径才能找到它。



