簡體   English   中英

無法使用MailChimp API 3.0將用戶添加到組

[英]Can't add user to a group using MailChimp API 3.0

我正在嘗試更新我的代碼以訂閱新用戶到我的MailChimp時事通訊。

我希望將用戶添加到一個組中,這樣我就可以在發送簡報時區分用戶。 但是,此代碼訂閱用戶,但不會將其添加到任何組。 我究竟做錯了什么? 我正在使用MailChimp API 3.0。

我的代碼:

$apiKey     = 'XXXX';
$list_id    = 'XXXX';
$memberId   = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);

$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . $memberId;

$json = json_encode(array(
    'email_address' => $data['email'],
    'status'        => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
    'merge_fields'  => array(
        'FNAME'         => $data['firstname'],
        'LNAME'         => $data['lastname'],
        'GROUPINGS'     => array(
            0 => array(
                'name'   => 'Interests',
                'groups' => array( $data['tag'] ),
            ),
        ),
    ),
));

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('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);

您正在錯誤的數組中傳遞興趣分組。 你需要傳遞interests數組中的興趣組ID,其值為true

$json = json_encode(array(
    'email_address' => 'test@example.com',
    'status'        => 'subscribed', 
    'merge_fields'  => array(
        'FNAME'         => 'FirstName',
        'LNAME'         => 'LastName',
     ),
    'interests'     => array( 
        '9143cf3bd1' => true,
        '789cf3bds1' => true
     ),
));

您將通過請求此URL獲取列表中的興趣組[ 9143cf3bd1,789cf3bds1 ]的ID

/lists/{list_id}/interest-categories

看這里的doc

如果要從組中刪除訂戶,則需要在更新請求中將其值設置為false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM