使用 Mailchimp 的 API v3 将订阅者添加到列表中 [英] Adding subscribers to a list using Mailchimp's API v3

查看:22
本文介绍了使用 Mailchimp 的 API v3 将订阅者添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add users to a list I've created in Mailchimp but I can't find any code examples anywhere. I've tried figuring out how to use the API but I'm very much a "Look at an example and learn" kind of person.

我已经尝试使用 API 的第 2 版,但尽管通过网络上的示例工作,但似乎没有任何效果,Mailchimp 在其网站上对早期版本的 API 说了以下内容:

I've tried using version 2 of the API but nothing seems to be working despite working from examples on the net and Mailchimp says the following about earlier versions of their API on their website:

不推荐使用 2.0 及更早版本.这些版本仅提供最低限度的支持(错误修复、安全补丁).

Versions 2.0 and earlier are deprecated. Only minimal support—bug fixes, security patches—will be available for those versions.

更新 1:我根据 TooMuchPete 的 answer 对关于管理订阅者的链接并更改了我在此处找到的一些代码,但它不起作用,因为函数 http_build_query() 不处理嵌套数组.我不确定如何处理添加订阅者的merge_fields"部分.我当前的代码如下:

UPDATE 1: I did some further research based on TooMuchPete's answer with regards to the link on Managing Subscribers and altered some code I found here, but it won't work because the function http_build_query() doesn't deal with nested arrays. I'm not sure how to deal with the 'merge_fields' portion of adding a subscriber. My current code is below:

$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 标头中.请参阅下面的代码:

UPDATE 2: I've now resorted to using curl and I've managed to get something almost working. The data sends through to Mailchimp but I'm receiving the error "Your request did not include an API key." I'm guessing I need to authenticate as mentioned here. I've tried adding it to the http header which hasn't worked. See my code below:

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

推荐答案

基于 列表成员实例文档,最简单的方法是使用 PUT 请求,根据文档要么添加新的列表成员或更新如果电子邮件已存在于列表中,则为成员".

Based on the List Members Instance docs, the easiest way is to use a PUT request which according to the docs either "adds a new list member or updates the member if the email already exists on the list".

此外,apikey 绝对不是 json 架构的一部分 并且没有必要将它包含在您的 json 请求中.

Furthermore apikey is definitely not part of the json schema and there's no point in including it in your json request.

此外,如@TooMuchPete 的评论所述,您可以使用 CURLOPT_USERPWD 进行基本的 http 身份验证,如下所示.

Also, as noted in @TooMuchPete's comment, you can use CURLOPT_USERPWD for basic http auth as illustrated in below.

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

I'm using the following function to add and update list members. You may need to include a slightly different set of merge_fields depending on your list parameters.

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

这篇关于使用 Mailchimp 的 API v3 将订阅者添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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