Ole Haugset Ole Haugset - 1 year ago 150
PHP Question

MailChimp API v2.0 subscribe

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?

Answer Source

Okay, so I was able to find the answer myself. The documentation on mailchimps behalf is partly wrong for the PHP api class they have set up. The key should infact 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download