我在下面的这段代码中将用户添加到Mailchimp中的预先存在的列表中。

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

此代码仅将用户添加到列表中,当我尝试两次添加同一用户的详细信息时,第二次尝试将引发以下错误:



如何使用PATCH更新用户详细信息?我不确定在哪里指定。

最佳答案

我弄清楚了我要去哪里错了。最初将用户添加到列表时,响应将提供一个ID。当我想在Mailchimp列表中更新用户的详细信息时,我需要将ID以及这些人的详细信息存储在数据库中,并在正在拨打的URL中引用该ID。

https://us2.api.mailchimp.com/3.0/lists/<list_id_goes_here>/members/<members_id_goes_here>

感谢@TooMuchPete提供正确的curl命令。
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");

关于php - 使用cURL和Mailchimp API v3更新列表中的订户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30531798/

10-12 00:20
查看更多