Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribing users with groups through MailChimp 2.0 API

Tags:

php

mailchimp

I've been working on this problem for hours and can't seem to find the solution, hopefully someone can help!

I'm trying to create a simple MailChimp subscribe form on an HTTPS site and hence must use the API. I am using the "super simple mailchimp-api" PHP wrapper they suggest, and am trying to add my subscribers to interest groups based on checkboxes selected in the form. All the relevant checkboxes are named "group[]" so that PHP will POST them as an array.

I am passing the below to the wrapper:

$MailChimp = new MailChimp('api_key_removed');
$result = $MailChimp->call('lists/subscribe', array(
                'id'                => 'list_ID_removed',
                'email'             => array( 'email' => $_POST['email'] ),
                'merge_vars'        => array( 'FNAME' => $_POST['fname'], 'LNAME' => $_POST['lname'], 'COMPANY' => $_POST['company'], 'STATE' => $_POST['state'], 
                                    'GROUPINGS' => array(
                                        array( 
                                            'ID' => 14093, 
                                            'GROUPS' => $_POST['group']
                                        ) 
                                    )
                ),
                'double_optin'      => false,
                'update_existing'   => true,
                'replace_interests' => false,
                'send_welcome'      => true
            ));

When I test, the users are created correctly but no interest groups are selected. I have double checked that both the grouping ID and group names are correct. I have even tried hardcoding an array for GROUPS to no avail.

An example of the merge_vars $args passed to the API are:

[merge_vars] => Array
    (
        [FNAME] => Test
        [LNAME] => Test
        [COMPANY] => 
        [STATE] => TAS
        [GROUPINGS] => Array
            (
                [0] => Array
                    (
                        [ID] => 14093
                        [GROUPS] => Array
                            (
                                [0] => Invest
                                [1] => Deposit Bonds
                            )

                    )

            )

    )

From what I can understand this is exactly right, so I'm just not understanding where the problem lies. Can anyone see what I'm doing wrong? Or is the API broken?

Thanks

Josh

like image 831
Josh Avatar asked Nov 28 '13 12:11

Josh


People also ask

How do I add a subscriber to Mailchimp via API?

To add a contact to an audience, you must include the subscription status in your payload: Use subscribed to immediately add a contact. Use pending to send a confirmation email. Once confirmed, the contact's status will update to subscribed .

What does API stand for Mailchimp?

API stands for application programming interface. It can be helpful to think of the API as a way for different apps to talk to one another. For many users, the main interaction with the API will be through API keys, which allow other apps to access your account without you giving out your password.


1 Answers

I arrived at this "unanswered" question with zero answers, but found it was actually already answered in the comments of the question. I'm copying the comments to reflect the question has already received an answer.


OMG, after 5h I just discovered the problem! For anyone else stumped by this - the keys (not values) "groupings", "id" and "groups" all need to be lower case. I really don't understand why when all the other fields are documented everywhere as being uppercase and indeed work when they are uppercase. I assume it's some cruel joke they enjoy playing on developers. – Josh Nov 28 '13 at 12:37


In the older API versions they were in upper case. They changed it in 2.0. – Daniel Rikowski Mar 16 '14 at 21:49

like image 185
jimp Avatar answered Nov 15 '22 15:11

jimp