添加用户使用Mailchimp的API V3列表 [英] Adding subscribers to a list using Mailchimp's API v3

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

问题描述

我试图将用户添加到我在Mailchimp创建的列表,但我无法找到任何code例子任何地方。我试着搞清楚如何使用API​​,但我这人非常看一个例子,了解什么样的人。

我已经使用API​​第2版尝试,但似乎没有任何尽管实例上工作网合作,并Mailchimp说,大约在其网站上的早期版本他们的API的以下内容:


  

版本2.0和更早版本的德precated。只有很少的支持,bug修复,安全补丁,将可用于这些版本。


更新1 :我没根据TooMuchPete的回答与问候的在管理订阅链接,并改变一些code,我发现这里,但它不会工作,因为函数<一个HREF =htt​​ps://php.net/http_build_query> http_build_query()不处理嵌套的数组。我不知道如何处理'merge_fields添加用户的一部分。我现在的code是如下:

  $ POSTDATA = http_build_query(
                    阵列(
                        apikey'=&GT; $ apikey,
                        EMAIL_ADDRESS'=&GT; $电子邮件,
                        状态=&GT; '认​​购',
                        merge_fields'=&GT;阵列(
                            FNAME'=&GT; $名称
                        )
                    )
                );                $ =选择采用阵列('HTTP'=&GT;
                    阵列(
                        '方法'= GT; POST,
                        '头'=&GT; 内容类型:应用程序/ x-WWW的形式urlen codeD,
                        '内容'=&GT; $ POSTDATA
                    )
                );                $语境= stream_context_create($ OPTS);                $结果=的file_get_contents('https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/',虚假,$背景);                后续代码var_dump($结果);
                死('Mailchimp执行');

更新2 :现在我已经诉诸使用curl,我已经成功地得到的东西差不多的工作。发送数据通过对Mailchimp但我收到错误的您的要求不包括API密钥。我猜我需要验证所提到的此处。我试着将它添加到HTTP头已经没有奏效。看到我的code以下:

  $ apikey ='&LT;&API_KEY GT;';
                $ AUTH = base64_en code('用户名:'$ apikey);                $数据=阵列(
                    apikey'=&GT; $ apikey,
                    EMAIL_ADDRESS'=&GT; $电子邮件,
                    状态=&GT; '认​​购',
                    merge_fields'=&GT;阵列(
                        FNAME'=&GT; $名称
                    )
                );
                $ json_data = json_en code($的数据);                $ CH = curl_init();
                curl_setopt($ CH,CURLOPT_URL,'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/');
                curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列('内容类型:应用程序/ JSON / R / N
                                                            授权:基本$ AUTH));
                curl_setopt($ CH,CURLOPT_USERAGENT,PHP-MCAPI / 2.0');
                curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
                curl_setopt($ CH,CURLOPT_TIMEOUT,10);
                curl_setopt($ CH,CURLOPT_POST,真正的);
                curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
                curl_setopt($ CH,CURLOPT_POSTFIELDS,$ json_data);                $结果= curl_exec($ CH);                后续代码var_dump($结果);
                死('Mailchimp执行');


解决方案

根据在列出成员实例文档,最简单的方法是使用 PUT 请求,根据文档或者添加如果电子邮件列表中的

上已存在新的列表成员或更新成员

此外 apikey 绝对不是的 JSON模式并没有点,包括它在你的JSON请求。

此外,如@ TooMuchPete的评论指出,可以使用 CURLOPT_USERPWD 基本HTTP认证如图所示,在下面。

我用下面的函数添加和更新列表的成员。您可能需要包括一套略有不同 merge_fields 根据您的列表参数。

  $数据= [
    电子邮件=&GT; johndoe@example.com',
    状态=&GT; '认​​购',
    '名字'=&GT; 约翰,
    '姓氏'=&GT; 母鹿
];syncMailchimp($数据);功能syncMailchimp($数据){
    $ apiKey ='你的API密钥';
    $ listId ='您的列表ID;    $ MEMBERID = MD5(用strtolower($数据[电子邮件]));
    $数据中心= SUBSTR($ apiKey,strpos($ apiKey,' - ')+ 1);
    $ URL =的https://。 $数据中心。 .api.mailchimp.com / 3.0 /列表/'。 $ listId。 '/会员/'。 $ MEMBERID;    $ JSON = json_en code([
        EMAIL_ADDRESS'=&GT; $数据[电子邮件]
        状态=&GT; $数据['状态'],//订阅,退订,清理,待定
        merge_fields'=&GT; [
            FNAME'=&GT; $数据['名字'],
            L-NAME =&GT; $数据['姓氏']
        ]
    ]);    $ CH = curl_init($网址);    curl_setopt($ CH,CURLOPT_USERPWD,'用户名:'$ apiKey);
    curl_setopt($ CH,CURLOPT_HTTPHEADER,['内容类型:应用程序/ JSON']);
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
    curl_setopt($ CH,CURLOPT_TIMEOUT,10);
    curl_setopt($ CH,CURLOPT_CUSTOMREQUEST,'把');
    curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ CH,CURLOPT_POSTFIELDS,$ JSON);    $结果= curl_exec($ CH);
    $ HTTP code = curl_getinfo($ CH,CURLINFO_HTTP_ code);
    curl_close($ CH);    返回$ HTTP code;
}

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.

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:

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

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

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

解决方案

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".

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

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

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天全站免登陆