Mailchimp API 3.0批量订阅 [英] Mailchimp API 3.0 Batch Subscribe

查看:111
本文介绍了Mailchimp API 3.0批量订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,到/lists/{list_id}的POST应该批量订阅或取消订阅列表成员.

Acconding to the documentation a POST to /lists/{list_id} should batch subscribe or unsubscribe list members.

如果我派两个成员;一名新成员和一名退订成员:

If I send two members; one new member and one unsubscribed member:

    {
        "update_existing":true,
        "members":[
            {
                "email_address":"yyyy@yyy.yy",
                "email_type":"html",
                "status":"subscribed"
            },
            {
                "email_address":"xxx@xxx.xx",
                "email_type":"html",
                "status":"subscribed"
            }
        ]
    }

文档( http://developer.mailchimp.com /documentation/mailchimp/reference/lists/#create-post_lists_list_id ),指出生成的JSON将包含具有new_members,updated_members的数组和具有错误的成员的数组:

The documentation(http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#create-post_lists_list_id), states that the resulting JSON will include an array with new_members, updated_members and an array with members with errors:

响应正文参数

Response body parameters

错误:对象数组,每个对象代表无法添加到列表或更新的电子邮件地址,以及提供更多详细信息的错误消息.

Errors: An array of objects, each representing an email address that could not be added to the list or updated and an error message providing more details.

.

但是相反,我得到了HTTP Status 400并显示以下错误:

But instead I get a HTTP Status 400 with the following error:

{
    "type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-  glossary/",
    "title":"Member Exists",
    "status":400,
    "detail":"xxx@xxx.xx is in a compliance state due to unsubscribe, bounce, or compliance review and cannot be subscribed.",
    "instance":""
}

推荐答案

对于 Batch subscribe ,请尝试下面的代码.这是经过测试的100%有效代码.

For Batch subscribe try this below code.This is tested and 100% working code.

<?php
$apikey  = ''; // Your Mailchimp ApiKey
$list_id = ''; // your List ID Where you want to add subscriber

$servername = 'localhost';
$username   = 'root';
$password   = '';
$dbname     = 'dada_mail_to_mailchimp';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die('Connection failed: ' . $conn->connect_error);
}

$sql       = 'SELECT * FROM emails Limit 2';
$result    = $conn->query($sql);
$finalData = [];
if ($result->num_rows > 0) {
    // output data of each row
    while ($row = $result->fetch_assoc()) {
        $individulData = array(
            'apikey'        => $apikey,
            'email_address' => $row['email'],
            'status'        => $row['status'],//subscribe,pending,unsubscribe
            'merge_fields'  => array(
                'FNAME' => $row['FNAME'],
                'LNAME' => $row['LNAME'],
            )
        );

        $json_individulData        = json_encode($individulData);
        $finalData['operations'][] =
            array(
                "method" => "POST",
                "path"   => "/lists/$list_id/members/",
                "body"   => $json_individulData
            );
    }
}

$api_response = batchSubscribe($finalData, $apikey);
print_r($api_response);
$conn->close();

/**
 * Mailchimp API- List Batch Subscribe added function
 *
 * @param array  $data   Passed you data as an array format.
 * @param string $apikey your mailchimp api key.
 *
 * @return mixed
 */
function batchSubscribe(array $data, $apikey)
{
    $auth          = base64_encode('user:' . $apikey);
    $json_postData = json_encode($data);
    $ch            = curl_init();
    $dataCenter    = substr($apikey, strpos($apikey, '-') + 1);
    $curlopt_url   = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/batches/';
    curl_setopt($ch, CURLOPT_URL, $curlopt_url);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
        'Authorization: Basic ' . $auth));
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_postData);

    $result = curl_exec($ch);
    return $result;
}

这篇关于Mailchimp API 3.0批量订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆