使用Mailchimp的API v3将订阅者添加到列表中

2022-08-30 09:04:12

我正在尝试将用户添加到我在Mailchimp中创建的列表中,但我在任何地方都找不到任何代码示例。我已经尝试过弄清楚如何使用API,但我是一个非常“看一个例子并学习”的人。

我尝试过使用API的版本2,但尽管从网络上的示例中工作,但似乎没有任何工作,Mailchimp在其网站上对其API的早期版本表示如下:

版本 2.0 及更早版本已弃用。只有最低限度的支持(错误修复、安全补丁)可用于这些版本。

更新1:我根据TooMuchPete关于管理订阅者链接的答案做了一些进一步的研究,并更改了我在这里找到的一些代码,但它不起作用,因为函数http_build_query()不处理嵌套数组。我不确定如何处理添加订阅者的“merge_fields”部分。我目前的代码如下:

$postdata = http_build_query(
                    array(
                        'apikey'        => $apikey,
                        'email_address' => $email,
                        'status'        => 'subscribed',
                        'merge_fields'  => array(
                            'FNAME' => $name
                        )
                    )
                );

                $opts = array('http' =>
                    array(
                        'method'  => 'POST',
                        'header'  => 'Content-type: application/x-www-form-urlencoded',
                        'content' => $postdata
                    )
                );

                $context  = stream_context_create($opts);

                $result = file_get_contents('https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/', false, $context);

                var_dump($result);
                die('Mailchimp executed');

更新2:我现在已经诉诸于使用curl,并且我已经设法获得了几乎有效的东西。数据发送到Mailchimp,但我收到错误“您的请求不包含API密钥”。我想我需要像这里提到的那样进行身份验证。我尝试将其添加到不起作用的http标头中。请参阅下面的代码:

$apikey = '<api_key>';
                $auth = base64_encode( 'user:'.$apikey );

                $data = array(
                    'apikey'        => $apikey,
                    'email_address' => $email,
                    'status'        => 'subscribed',
                    'merge_fields'  => array(
                        'FNAME' => $name
                    )
                );
                $json_data = json_encode($data);

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/');
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json/r/n
                                                            Authorization: Basic '.$auth));
                curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.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_data);                                                                                                                  

                $result = curl_exec($ch);

                var_dump($result);
                die('Mailchimp executed');

答案 1

基于列表成员实例文档,最简单的方法是使用 PUT 请求,根据文档,该请求“添加新的列表成员或更新成员(如果电子邮件已存在于列表中)。”

此外,它绝对不是json架构的一部分,在json请求中包含它也没有意义。apikey

此外,如@TooMuchPete的评论中所述,您可以用于基本的http身份验证,如下图所示。CURLOPT_USERPWD

我正在使用以下函数来添加和更新列表成员。您可能需要根据列表参数包含一组略有不同的参数。merge_fields

$data = [
    'email'     => 'johndoe@example.com',
    'status'    => 'subscribed',
    'firstname' => 'john',
    'lastname'  => 'doe'
];

syncMailchimp($data);

function syncMailchimp($data) {
    $apiKey = 'your api key';
    $listId = 'your list id';

    $memberId = md5(strtolower($data['email']));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;

    $json = json_encode([
        'email_address' => $data['email'],
        'status'        => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
        'merge_fields'  => [
            'FNAME'     => $data['firstname'],
            'LNAME'     => $data['lastname']
        ]
    ]);

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                                                                 

    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $httpCode;
}

答案 2

我让它工作了。我错误地将身份验证添加到标头:

$apikey = '<api_key>';
            $auth = base64_encode( 'user:'.$apikey );

            $data = array(
                'apikey'        => $apikey,
                'email_address' => $email,
                'status'        => 'subscribed',
                'merge_fields'  => array(
                    'FNAME' => $name
                )
            );
            $json_data = json_encode($data);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/');
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
                                                        'Authorization: Basic '.$auth));
            curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.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_data);                                                                                                                  

            $result = curl_exec($ch);

            var_dump($result);
            die('Mailchimp executed');

推荐