77

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

使用他们的基本时事通讯注册表单……您只需将一些预先打包的 HTML 嵌入到您的页面中。然而,这样做的问题是单击提交重定向到 MailChimp 页面。(我不想重定向到 MailChimp,我希望用户在点击提交后留在自己的网站上。

它们提供 API 和大量文档,但有用的示例几乎为零。API 应该允许我与我的站点或应用程序进行完全集成。似乎当我在他们的文档中阅读适用于我的内容时,我单击链接以获取更多信息,最终我会绕着圈子转。他们告诉你怎么做,但他们没有“告诉”你怎么做。

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

这里关于他们的预打包解决方案的困惑是我只有一个常规的静态 HTML 页面,它不是 Wordpress、PHP 或 Drupal ......所以我只是不知道从哪里开始......我什至不知道如果我应该使用POSTor GET

我不是 API 的新手......我在让 Google Maps API 做任何我想做的事情方面做得很好。然而,除了我学习它的详细文档之外,谷歌还提供了真实世界的工作示例。在掌握 API 的细节之前,我只想看看它的实际效果。

在他们的在线文档中没有任何可靠的示例或教程,我正在询问如何使用他们的 API 创建最基本的 HTML 注册表单。

4

3 回答 3

75

编辑:

自发布此答案以来,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!&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) 创建您的 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中的 url 路径以便它可以找到它。

于 2011-02-20T19:22:47.800 回答
20

这是一个使用2.0 版Mailchimp API 和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 调用发送的内容。

于 2013-09-18T12:47:13.300 回答
8

这是使用官方 PHP Wrapper使用 Mailchimp API 2.0 版的另一个示例。

我的示例与此处发布的其他示例之间的区别在于,我使用的是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
}
于 2013-12-05T10:55:56.707 回答