首页 文章

Mailchimp 3.0 PHP直接在没有选择加入的情况下添加订阅者

提问于
浏览
0

我的问题是使用Mailchimp 3.0 API和PHP将订阅者直接添加到我的邮件列表中 .

我正在使用的代码(见下文)按预期运行并添加订户 . 但是,订户会收到选择加入的电子邮件 .

screenshot of test

代码来自:http://www.johnkieken.com/mailchimp-form-using-api-v3-0-and-jquery-ajax/

所需的行为是在没有选择加入电子邮件的情况下将订户直接添加到列表中,并在他们已成功订阅的站点上提供消息 .

我的服务器运行PHP 5.3.16 .

我有HTML文件,Mailchimp API包装器(mailchimp.php)和subscribe.php都驻留在同一目录中以进行测试 .

我不熟悉编码,所以我希望有人可以提供帮助 .

HTML

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<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: 'subscribe.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>
</body>
</html>

subscribe.php

<?php // for MailChimp API v3.0

include('MailChimp.php');  // path to API wrapper downloaded from GitHub

use \DrewM\MailChimp\MailChimp;

function storeAddress() {

    $key        = "mymailchimpAPIkey-us17";
    $list_id    = "mymailchimplistid";

    $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';
}

如果我编辑subscribe.php到:

// 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
    )
);

我收到以下消息:

“错误:email@domain.com已经是列表成员 . 使用PUT插入或更新列表成员 . ”

1 回答

  • 0

    看起来您尝试 POST 的订阅者已经是列表成员(可能处于挂起状态) . 也许是从早期 POST 那里 'status' => 'pending' . 在这些情况下,您需要发出 PUTPATCH 请求才能更新MailChimp中已有的联系人 . 这是因为 POST 请求仅用于添加新记录 . 可以在此处找到列表成员 endpoints 的 PUT 请求的文档:

    http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash

    看起来你正在使用 drewm/mailchimp-api . 我对这个库并不是很熟悉,但看起来你可以调用 $mc->patch(...)$mc->put(...) .

    如果您发出 PUT 请求,那么MailChimp将更新电子邮件地址的记录(如果已存在),如果不存在则会创建 . 所以听起来你应该坚持 PUT 请求你所描述的上述行为 .

相关问题