Okay, so I'm trying to add people to an existing mailing list in MailChimp using the MailChimp API v2.0 and the MailChimp-php-api.
I have the following code which, as I read their documentation should be correct. However, I get an error on it every time:
$MailChimp = new Mailchimp( 'my_api-key' );
try {
$retval = $MailChimp->lists->subscribe( 'my_api-key', 'my_list_id', array( 'email' => $checkoutDetails['EMAIL'] ) );
do_action( 'logit', 'LOG', 'MailChimp subscribe:', json_encode( $retval ), __METHOD__ );
} catch( Mailchimp_ValidationError $e ){
do_action( 'logit', 'ERROR', 'MailChimp error:', 'Error: ' . $e->getMessage(), __METHOD__ );
}
And every time I log this error:
Error: Validation error: {"email":"Please enter a struct\/associative array"}.
The documentation I followed is written here: https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php
Anyone got any clue what's going wrong?
Okay, so I was able to find the answer myself.
The MailChimp documentation is partly wrong for the PHP API class they have set up. The key should in fact not be included as the first parameter of the subscribe() function. Removing it so that the array containing the email became the second parameter fixed it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With