使用他们的 API 创建一个基本的 MailChimp 注册表单

2024-05-22

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

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

他们提供了 API 和大量文档,但有用的示例几乎为零。 API 应该允许我与我的网站或应用程序进行完全集成。似乎当我在他们的文档中读到一些适用于我的内容时,我单击链接以获取更多信息,但最终却陷入了原地踏步。他们告诉你如何去做,但他们没有“展示”你如何去做。

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

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

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

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


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!&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>

所需件...

  • 索引.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所以它可以找到它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用他们的 API 创建一个基本的 MailChimp 注册表单 的相关文章

随机推荐