8

I'm trying to update my code for subscribing new users to my MailChimp newsletter.

I would like it to add the user to a group, so that I can distinguish between users when sending out newsletters. This code, however, subscribes the user but does not add it to any group. What am I doing wrong? I am using the MailChimp API 3.0.

My code:

$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);
ekad
  • 14,436
  • 26
  • 44
  • 46

1 Answers1

16

You are passing Interest grouping in the wrong array. you need to pass the interest group id in interests array with value as 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
     ),
));

You will get the id of interest groups [9143cf3bd1,789cf3bds1 in above example] of the list by requesting to this URL

/lists/{list_id}/interest-categories

see doc here

If you want to remove the subscriber from the group, you need to set its value to false in your update request.

Mûhámmàd Yäsår K
  • 1,492
  • 11
  • 24
  • Hi Mûhámmàd, My code is: array( 'email_address' => 'test@example.com', 'status' => 'pending', 'merge_fields' => array( 'FNAME' => 'FirstName', 'LNAME' => 'LastName', ), 'interests' => array( '8c4bb7fbfd' => true ), ) But it still doesn't add the user to the interest group. It gives me a 200 header though. – Tobias Christian Jensen Aug 08 '16 at 08:27
  • Glad to know that @TobiasChristianJensen :) – Mûhámmàd Yäsår K Aug 18 '16 at 12:32
  • I'm trying the same thing but I'm getting *400 Bad request* error. and message is: Invalid interest ID: '8f14b8ad10'. – Omer Oct 03 '17 at 06:32
  • Am I adding the ID wrong... I got the `"list_id": "8f14b8ad10",` from this GET request: /lists/{list_id}/interest-categories – Omer Oct 03 '17 at 06:33
  • Tried recently, looks like `interests` array should not have direct keys, but arrays instead: `'interests' => array( array( '9143cf3bd1' => true), array('789cf3bds1' => true) ),` – Kos Jan 15 '19 at 09:23
  • @Kos You are wrong. Just tested it. The answer has the correct format. – Towfiq Feb 01 '21 at 17:44