Php 如何在MailChimp API 3中更新现有订阅服务器的段和组

Php 如何在MailChimp API 3中更新现有订阅服务器的段和组,php,mailchimp,mailchimp-api-v3.0,Php,Mailchimp,Mailchimp Api V3.0,Iv'e编写了一个方法,用于向用户订阅MailChimp。它的“特别之处”在于,它会根据用户的购物车项目、愿望列表项目以及他订阅的项目和/或类别,自动为用户订阅列表中的组和列表中的段 与MailChimp的集成是直接的-我获取数据>发送卷曲>获取响应>处理响应 我正在寻找一种根据用户在我的商店中的行为不断更新用户组和细分的方法 现在,MailChimp可以获得的唯一可接受状态是“已订阅”、“待定”和“清除”。它们都没有更新,只是插入了新的订阅者。如果该电子邮件已订阅,则不会更新任何内容,甚至不会

Iv'e编写了一个方法,用于向用户订阅MailChimp。它的“特别之处”在于,它会根据用户的购物车项目、愿望列表项目以及他订阅的项目和/或类别,自动为用户订阅列表中的组和列表中的段

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

如果您的集成按预期添加了全新的订阅服务器,并且问题似乎与方法更新现有sub记录的情况无关,那么问题可能与HTTP方法和/或api端点有关

MailChimp API的v3只允许在使用POST方法时初始化订阅者(看起来它可能在这里硬编码为cURL),这可能就是为什么添加全新订阅者时不会出现问题的原因

也就是说,当想要使用PUT添加或更新新订阅者时,建议使用PUT,并在他们的文档中指定

此外,为了确保现有订阅者得到更新,除了使用此替代方法外,还需要将其电子邮件小写版本的MD5哈希附加到端点这只需要对现有潜艇执行。

e、 g.
/members/{lowercase\u email\u MD5\u hash}


如果您首先使用MailChimp检查是否存在订阅者,如果您希望回收该订阅者,则应在响应中提供

如果您的集成按预期添加了全新的订阅服务器,并且问题似乎与方法更新现有sub记录的情况无关,那么问题可能与HTTP方法和/或api端点有关

MailChimp API的v3只允许在使用POST方法时初始化订阅者(看起来它可能在这里硬编码为cURL),这可能就是为什么添加全新订阅者时不会出现问题的原因

也就是说,当想要使用PUT添加或更新新订阅者时,建议使用PUT,并在他们的文档中指定

此外,为了确保现有订阅者得到更新,除了使用此替代方法外,还需要将其电子邮件小写版本的MD5哈希附加到端点这只需要对现有潜艇执行。

e、 g.
/members/{lowercase\u email\u MD5\u hash}


如果您首先使用MailChimp检查是否存在订阅者,如果您希望回收该订阅者,则应在响应中提供

真棒,伙计,它完全按照你解释的那样工作。谢谢你的帮助:)很高兴听到这个@ShirEkerling:-)很高兴。我该怎么做?:]哈哈,事实上我也不知道。/太棒了,伙计,它完全按照你解释的那样工作。谢谢你的帮助:)很高兴听到这个@ShirEkerling:-)很高兴。我该怎么做?:]哈哈,其实我也不知道:/