I'm trying to create a snippet for formit that will submit the data in the form to mailchimp as a new subscriber. I'm just not very expirienced with PHP and I seem to be running into trouble. Every time I submit the form, it brings me back to the form and no user is added to the email ist. No error is generated in the log either. The formit2db/db2formit work perfectly, it's the addition of this snippet that messes it up (hence the packageName parameter).
I took the examples from mailchimp's website and modified it for the snippet code so the basics SHOULD be right. Does anyone have any experience with the MailChimp API? I have never tried to create a snippet before so I'm new at this and may be messing up something really basic. It would make my life so much easier if I didn't have to regularly export mysql tables and import them into mailchimp.
Examples of API use:
http://apidocs.mailchimp.com/api/downloads/#examples
$packageName = $scriptProperties['packageName'];
$packagepath = $modx->getOption('core_path') . 'components/'.$packageName.'/';
$modelpath = $packagepath.'model/';
/*for formit2db but it's the directory where I place the CAPI.class.php file */
$volunteer = $hook->getValue('volunteer');
if ($volunteer == 'Yes') {
function storeAddress(){
require_once($modelpath.'/MCAPI.class.php');
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('MyApiKey');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "MyListID";
// Merge variables are the names of all of the fields your mailing list accepts
// Ex: first name is by default FNAME
// You can define the names of each merge variable in Lists > click the desired list > list settings > Merge tags for personalization
// Pass merge values to the API in an array as follows
$mergeVars = array('FNAME'=>$hook->getValue('name'),
'ADD1'=>$hook->getValue('address'),
'ADD2'=>$hook->getValue('address2'),
'CITY'=>$hook->getValue('city'),
'STATE'=>$hook->getValue('state'),
'ZIP'=>$hook->getValue('zip'),
'COUNTY'=>$hook->getValue('county');
if($api->listSubscribe($list_id, $scriptProperties['email'];, $mergeVars) === true) {
// It worked!
return true;
}else{
// An error ocurred, return error message
$modx->log(modX::LOG_LEVEL_ERROR,'[FormIt] mailchimpAPI could complete the addition of user to list.');
return false;
}
}
else {
return true;
}