簡體   English   中英

如何在MailChimp API 3中更新現有訂戶的段和組

[英]How to update segments and groups of an existing subscriber in MailChimp API 3

我寫了一個方法來訂閱用戶MailChimp。 關於它的特殊之處在於它根據用戶的購物車項目,願望清單項目以及他訂購的項目和/或類別,自動將用戶訂閱到列表中的組和列表中的段。

與MailChimp的集成是直截了當的 - 我得到數據>發送curl>獲取響應>處理響應。

我正在尋找一種方法來根據他們在我的商店中的操作不斷更新用戶的組和細分。

現在,MailChimp可以獲得的唯一接受狀態是“訂閱”,“待定”和“清理”。 所有這些都沒有更新,只插入新訂閱者。 如果電子郵件已經訂閱,則不會更新任何內容,甚至不會更新與訂閱者在其MailChimp列表中的配置文件中所擁有的數據不同的數據。

這是我的代碼供參考:

    protected static function subscribeToMailchimp($email, $fullname)
{
    $params     = EkerbaseJoomla::getPluginParams('system', 'ekerbaseusers');
    $interests  = self::getUserInterestsObject();

    $apikey                 = $params->mailChimpApiKey;
    $listId                 = $params->mailChimpListId;
    $interestCategoryId     = $params->mailChimpInterestCategoryId;

    $auth                   = base64_encode( 'user:' . $apikey );
    $apiUrl                 = 'https://'.substr($params->mailChimpApiKey, -3).'.api.mailchimp.com/3.0/lists/'.$listId;
    $possibleGroups         = json_decode(file_get_contents($apiUrl . '/interest-categories/' . $interestCategoryId . '/interests?apikey=' . $apikey))->interests;
    $segments               = json_decode(file_get_contents($apiUrl . '/segments?apikey=' . $apikey))->segments;

    $data = [
        'apikey'            => $apikey,
        'email_address'     => $email,
        'status'            => 'subscribed',
        'merge_fields'      =>
            [
                'FNAME'     => $fullname
            ]
    ];

    if( ! empty($interests->categories) ) {

        $data['interests'] = [];

        foreach( $possibleGroups as $group ) {

            if( in_array($group->name, $interests->categories) ) {
                $data['interests'][$group->id] = true;
            }

        }

    }

    if( ! empty($interests->items) ) {

        $data['segments'] = [];

        foreach( $segments as $segment ) {

            if( in_array($segment->name, $interests->items) ) {
                $data['segments'][$segment->id] = true;
            }

        }

    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $apiUrl . '/members/');
    curl_setopt($ch, CURLOPT_HTTPHEADER,
        [
            'Content-Type: application/json',
            'Authorization: Basic '.$auth
        ]
    );

    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.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_encode($data));

    $result     = curl_exec($ch);
    $response   = json_decode($result);

    switch( $response->status ) {

        case 'subscribed':
            $responseMessage = JText::_('EKERBASE_SUCCESS_NEWSLETTER');
            $responseStatus  = 'success';
            $responseResult  = 'mailchimp subscribing succeeded';
            break;

        default:

            $responseStatus  = 'error';
            $responseMessage = $response->title;
            $responseResult  = 'mailchimp subscribing failed';

            if( $response->title === 'Member Exists' ) {
                $responseMessage = JText::_('EKERBASE_NEWSLETTER_ALREADY_SUBSCRIBER');
            }

            break;

    }

    return EkerbaseAjax::buildJsonResponse($responseMessage, $responseStatus, $responseResult);
}

如果您的集成按預期添加了全新的訂閱者,並且該問題在該方法更新現有子記錄的情況下顯得孤立,則該問題可能與HTTP方法和/或api端點有關。

由於MailChimp API的v3僅允許在使用POST方法時初始化訂閱者(這看起來可能在此處硬編碼到cURL中),並且可能是為什么全新訂閱者被添加而沒有問題。

這就是說,當想要使用PUT添加或更新新訂戶時,建議使用,並在其文檔中指定。

此外,除了這種替代方法的使用,為了確保更新現有訂戶,您還需要將其電子郵件小寫版本的MD5哈希附加到端點。 這只需要對現有的潛艇進行。

例如/members/{lowercase_email_MD5_hash}

如果您首先使用MailChimp檢查是否存在訂戶,如果​​您想要回收該訂戶,則應在響應中提供。

暫無
暫無

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

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