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版,但似乎没有什么效果,尽管从网络上的例子来看,Mailchinp在他们的网站上说了以下关于他们API的早期版本的内容:

不推荐使用2.0及更早版本.这些版本只提供最低限度的漏洞修复和安全补丁.

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:我现在求助于使用curl,我已经设法让一些东西几乎可以工作了.数据发送到Mailchimp,但我收到了错误"Your request did not include an API key.",我想我需要进行身份验证.我try 将其添加到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');

推荐答案

List Members Instance docs的基础上,最简单的方法是使用101请求,根据文档,这可以是"adds a new list member or updates the member if the email already exists on the list".

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

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

Json相关问答推荐

JOLT拉平数组

使用Jolt变换转换JsonArray以将关键字转移到内部JsonArray中

JOLT将对象名作为新属性添加到主体中

使用动态和可变JSON的图表多条

将数组中的值作为键连接到另一个数组中的值(Jolt)

当由.sh脚本执行时,AWS查询字符串不会提取任何数据

解析Ansible AWS ec2_security_group中的json以提取安全组ID

bash用jq获取第二条JSON记录

使用Powershell将JSON文件加载到SQL Server表格

基于JQ中另一个对象的值 Select 对象

如何使用 jq 将字符串数组转换为对象?

如何 Select 一个值,这是可选的 - 使用 jq

Json.NET SerializeObject 转义值以防止 XSS

从 JSON 对象中删除键值对

Swift:如何从字典中删除空值?

可以通过 POST 使用 EventSource 传递参数的服务器发送事件 (SSE)

JSON RPC - 什么是id?

使用 Node.js 对 JSON 中的字符串大小有限制吗?

如何在 Python 中合并两个 json 字符串?

通过url获取json数据并在python中使用(simplejson)