Search code examples
mailchimpmailchimp-api-v3.0mailchimp-api-v3

Mailchimp API 3.0 batch/bulk untag


I have a problem with untag the subscribers in a bulk with the Mailchimp API.

In the documentation https://mailchimp.com/developer/guides/how-to-use-tags/#Tag_multiple_contacts_in_bulk is the example:

{
    "members_to_remove": [
        "[email protected]",
        "[email protected]"
        ]
}

Below can you see my PHP code where I with the $methode variable give the value members_to_remove and the $email value is an array with email addresses.

But the script does only add tags with a bulk to the subscriptions instead of remove.

What do I wrong?

public function tag_mailchimp($list_id, $email, $tag, $method) {

    $authToken = 'HERE MY KEY';
    // The data to send to the API


    $postData = array(
        $method => $email
    );

    // Setup cURL
    $ch = curl_init('https://us2.api.mailchimp.com/3.0/lists/'.$list_id.'/segments/'.$tag);

    curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
            'Authorization: apikey '.$authToken,
            'Content-Type: application/json'
        ),
        CURLOPT_POSTFIELDS => json_encode($postData)
    ));

    // Send the request
    $response = curl_exec($ch);

    return $response;
}

Solution

  • It works, but I don't know what I do else.., the code:

    foreach($members as $member) {
    
       $list[] = $member->email;
    
    }
    
    $Mailer->tag_mailchimp('12345678', $list, 123456, 'members_to_remove');
    

    And in the class I have the next function:

    public function tag_mailchimp($list_id, $email, $tag, $method) {
    
            $authToken = 'HERE MY KEY';
            // The data to send to the API
    
    
            $postData = array(
                $method => $email
            );
    
            // Setup cURL
            $ch = curl_init('https://us2.api.mailchimp.com/3.0/lists/'.$list_id.'/segments/'.$tag);
    
            curl_setopt_array($ch, array(
                CURLOPT_POST => TRUE,
                CURLOPT_RETURNTRANSFER => TRUE,
                CURLOPT_HTTPHEADER => array(
                    'Authorization: apikey '.$authToken,
                    'Content-Type: application/json'
                ),
                CURLOPT_POSTFIELDS => json_encode($postData)
            ));
    
            // Send the request
            $response = curl_exec($ch);
    
            return $response;
        }