Home » Php » php – Add users to interest group via MailChimp API v2.0

php – Add users to interest group via MailChimp API v2.0

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want to add a subscriber to an interest group via the MailChimp API.

This is my $merge_vars array:

$merge_vars = array(
    'GROUPINGS' => array(
        0 => array(
            'id' => 17385, // The group list ID
            'groups' => "Test 123", // A test group, that does exist
        )
    )
);              

and this is how I’m updating the member:

$update = $mc->lists->updateMember(self::$mainListID, $email, $merge_vars);

Here’s a var_dump($merge_vars):

array(1) {
  ["GROUPINGS"]=>
  array(1) {
    [0]=>
    array(2) {
      ["id"]=>
      int(17385)
      ["groups"]=>
      string(8) "Test 123"
    }
  }
}

and $email is a struct, here’s $var_dump($email):

array(1) {
  ["email"]=>
  string(11) "[email protected]"
}

I’m about to be driven to distraction, because the API doesn’t return an error, everything seems to go smoothly, except for the big problem of the user not being added to the list.

I’ve looked at this question which helped me get so far, but the version of the API it uses is 1.3 and that might have something to do with it.

What am I doing wrong?

How to&Answers:

Well, I figured it out.

Although I could have sworn I’d already tried it this way… the groups have to be an array, even for a single group.

My code now:

$merge_vars = array(
    'GROUPINGS'=> array(
        array(
            'id' => 17385,
            'groups' => array($post['listName'])
        )
    )
);

$mc->lists->updateMember(self::$mainListID, $email, $merge_vars);

Works perfectly.

Answer:

For me works using name instead of id group.

$merge_vars = array(
    'groupings'=> array(
        array(
            'name' => 'Group Name',
            'groups' => array('Group item name')
        )
    )
);

Answer:

Agree with @Sanaco

add this example for more checkboxes

$option1=$_POST['xxxx'];
$option2=$_POST['xxxx'];
$option3=$_POST['xxxx'];

'GROUPINGS'=> array(
                            array(
                                'id' => 123456,
                                'groups' => array($option1, $option2, $option3)
                            )
                        )