使用他们的API创build一个基本的MailChimpregistry单

我是MailChimp的新手,需要一些帮助。

使用他们的基本时事通讯registry格,您只需将一些预先打包的HTMLembedded到您的页面中即可。 然而,这个问题是,点击提交redirect到MailChimp页面。 ( 我不想redirect到MailChimp,我希望用户在提交后留在自己的网站上。

他们提供了一个API和大量的文档,但几乎没有有用的例子。 该API应该让我做我的网站或应用程序的完整集成。 看来,当我阅读适用于我的文档中的某些内容时,我点击链接以获取更多信息,最后我会围着圈子走。 他们告诉你如何去做,但他们不能“展示”你如何去做。

我可以得到一个API密钥,他们有大量的文档,以及一大堆的包装和插件… PHP,Drupal,Wordpress等…

这里关于他们预先打包的解决scheme的困惑是,我只是有一个普通的静态HTML页面,这不是Wordpress,PHP或Drupal的…所以我只是不知道从哪里开始…我甚至不知道如果我应该使用POSTGET

我不是API的新手…我很好地获得Google Maps API来做我想做的事情。 但是,Google除了提供详细的文档外,还提供了实际的工作示例,这些文档是我学习的。 我只是想看看它在行动之前,我可以掌握API的更好的点。

在他们的在线文档中没有任何可靠的示例或教程,我在问如何使用他们的API创build最基本的HTMLregistry单。

编辑:

自发布这个答案MailChimp发布了他们的API的版本2和3。 版本3将是2017年开始支持的唯一版本。只要我有机会进行testing,我将更新API版本3的答案。


MailChimp API v3.0

根据本页顶部的通知, 2016年之后,API的所有先前版本将不再受支持。

我的解决scheme在后台使用PHP来处理API,使用jQuery来实现Ajax。

1)下载一个支持API v3.0的PHP包装器。 在撰写本文时,最新的MailChimp文档中没有任何支持v3.0的官方文档,但是有几个文档在GitHub上列出,所以我select了这个文档。

2)使用您自己的API密钥和列表ID创build以下PHP文件store-address.php ,然后将其放在与步骤1中的包装器相同的目录中。 请记住按照你的包装文件,但他们都看起来很相似。

 <?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)创build你的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示例的网站。 从那里我能够创build一个简单的HTML页面与jQuery包含基本registry单。 PHP文件被隐藏在后台,用户从来不会看到它们,但jQuery仍然可以访问和使用。

1)在这里下载PHP 5的jQuery示例…( 编辑 :链接是死的,但唯一重要的部分是PHP的官方API包装在这里可用。

downloads/mcapi-simple-subscribe-jquery.html

如果您只有PHP 4,只需下载MCAPI的1.2版本,并replace上面相应的MCAPI.class.php文件。

downloads/mcapi-simple-subscribe-jquery.html

2)按照自述文件中的说明,将API密钥和列表ID添加到store-address.php文件的正确位置。

3)你也可能想收集你的用户的名字和/或其他信息。 您必须使用相应的合并variables将数组添加到store-address.php文件。

这里是我的store-address.php文件的样子,我也收集名字,姓氏和电子邮件types:

 <?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!&nbsp; Check your inbox or spam folder for a message containing a confirmation link.'; } else { // An error ocurred, return error message return '<b>Error:</b>&nbsp; ' . $api->errorMessage; } } // If being called via ajax, autorun the function if($_GET['ajax']) { echo storeAddress(); } 

4)创build你的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 KEYLIST ID进行修改 。 您需要将其他可选字段添加到数组中。

  • 从Mailchimp站点下载的MCAPI.class.php文件(PHP 5版本1.3或PHP 4版本1.2)。 将其放在与store-address.php相同的目录中,或者必须更新store-address.php中的urlpath,以便find它。

以下是使用Mailchimp API 版本2.0与mailchimp-api (一个用于处理Mailchimp API的最小PHP抽象类)的示例。

 <?php include('MailChimp.php'); $MailChimp = new MailChimp('API_KEY'); $result = $MailChimp->call('lists/subscribe', array( 'id' => 'LIST_ID', 'email' => array( 'email' => $_POST['email'] ), 'merge_vars' => array( 'MERGE2' => $_POST['name'] // MERGE name from list settings // there MERGE fields must be set if required in list settings ), 'double_optin' => false, 'update_existing' => true, 'replace_interests' => false )); if( $result === false ) { // response wasn't even json } else if( isset($result->status) && $result->status == 'error' ) { // Error info: $result->status, $result->code, $result->name, $result->error } ?> 

在MailChimp API文档中详细了解可以使用API​​调用发送的内容。

这是使用官方PHP包装器使用2.0版Mailchimp API的另一个例子。

我的例子和这里发布的其他人之间的区别是,我使用Mailchimp_Lists类的subscribe方法,可以通过实例化Mailchimp类( ->lists ),而不是通用的调用方法。

 $api_key = "MAILCHIMP_API_KEY"; $list_id = "MAILCHIMP_LIST_ID"; require('Mailchimp.php'); $Mailchimp = new Mailchimp($api_key); $subscriber = $Mailchimp->lists->subscribe($list_id, array('email' => $_POST['email'])); if ( ! empty($subscriber['leid'])) { // Success }