MailChimp API PHP - Add to Interest Group

Joe picture Joe · Jan 6, 2012 · Viewed 16.9k times · Source

I'm currently using the MailChimp API for PHP, version 1.3.1 (http://apidocs.mailchimp.com/api/downloads/#php)

I've set up a list in MailChimp, and would like to dynamically add:

  1. Subscribers to the list (done: $objMailChimp->listBatchSubscribe($strMailingListID, ...))
  2. Interest Groupings (done: $objMailChimp->listInterestGroupingAdd($strMailingListID, ...))
  3. Interest Groups into those Groupings (done: $objMailChimp->listInterestGroupAdd($strMailingListID, ...))
  4. Subscribers assigned to relevant Groups (not done)

The API (http://apidocs.mailchimp.com/api/1.3/#listrelated) is somewhat unclear on how to add a subscriber to an interest group - does anyone here have any ideas?

Answer

Trevor Gehman picture Trevor Gehman · Sep 12, 2013

As of version 2.0 of MailChimp's API, this should work:

$merge_vars = array(
    'GROUPINGS' => array(
        array(
            'name' => "GROUP CATEGORY #1", // You can use either 'name' or 'id' to identify the group
            'groups' => array("GROUP NAME","GROUP NAME")
        ),
        array(
            'name' => "GROUP CATEGORY #2",
            'groups' => array("GROUP NAME")
        )
    )
);

Source: http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

Using a barebones PHP wrapper (https://github.com/drewm/mailchimp-api/) you can then send this to MailChimp via either the lists/subscribe or lists/batch-subscribe:

$MailChimp = new MailChimp('API_KEY');
$result = $MailChimp->call('lists/subscribe', array(
      'id'                => 'LIST ID',
      'email'             => array('email'=>'[email protected]'),
      'merge_vars'        => $merge_vars
));