I have implemented with mailchimp - it is quite simple, but unfortunetly i can not share the complete code as:
a. it contains customer specific implementations for extended user
b. it is defently more complex then what’s needed for an average implementation, as there are thousands of users - new users and modifications are synced in batches every hour (message-queue, croned). and some specific things are immediate event-driven (for specific fields, via processors).
This info should get you started:
1. /core/components/mailchimp/inc/MCAPI.class.php (
http://www.mailchimp.com/api/downloads/) - note: i used v1.2, and v1.3 has been recently released.
2. /core/components/mailchimp/mailchimp.config.php
(you could have it as modx settings, but i prefer a config file).
<?php
// MAIL CHIMP CONFIG
$mcApiKey = '**YOUR API KEY HERE***';
$mcListId = '**YOUR LIST ID HERE***';
// other default params
$mcDblOptIn = false;
$mcUpdateExisting = true;
$mcReplaceInterests = true;
?>
Example of changing user email address:
<?php
require_once 'inc/MCAPI.class.php';
require_once 'mailchimp.config.php';
$oldEmail = $_POST['oldEmail'];
$newEmail = $_POST['newEmail'];
if (empty($oldEmail) || empty($newEmail)) return;
$merge_vars = array('EMAIL' => $newEmail);
$mcApi = new MCAPI($mcApiKey);
$returnedValue = $mcApi->listUpdateMember($mcListId, $oldEmail, $merge_vars, '', false);
?>
Using this example - you’ll understand how to do whatever is in the API (
http://www.mailchimp.com/api/1.2/)
(partial) Example of batch creating/updating multiple users (used with cron):
<?php
// MAIL CHIMP CONFIG
require_once 'inc/MCAPI.class.php';
require_once 'mailchimp.config.php';
$memberIdList = array(); // contains a list of all memeber id to be synced
// ..here comes your code to populate the above array with all the user id's you want..
if (empty($memberIdList)) return;
$batchSubscribe = array();
foreach ($memberIdList as $memberId) { // construct batch array with member details
$memberUser = $modx->getObject('modUser', $memberId);
$memberProfile = $memberUser->getOne('Profile');
if (!isset($memberProfile)) return;
$memberMcArray = array(
'EMAIL' => $memberProfile->get('email'),
'FULLNAME' => $memberProfile->get('fullname'),
'CITY' => $memberProfile->get('city'),
'GENDER' => ($memberProfile->get('gender') === 1)? 'Male' : (($memberProfile->get('gender') === 2)? 'Female' : '' )
// ** etc... add here all your mailchimp fields **
);
$batchSubscribe[] = $memberMcArray;
}
if (empty($batchSubscribe)) return;
// ** DO IT **
$mcApi = new MCAPI($mcApiKey);
$vals = $mcApi->listBatchSubscribe($mcListId,$batchSubscribe,$mcDblOptIn, $mcUpdateExisting, $mcReplaceInterests);
// OUTPUT results with echo to a log file (it's cron'ed)
echo "\n** ".date("d/m/y")."\n";
if ($mcApi->errorCode){
echo "Batch Subscribe failed!\n";
echo "code:".$mcApi->errorCode."\n";
echo "msg :".$mcApi->errorMessage."\n";
} else {
echo "success:".$vals['success_count']."\n";
echo "errors:".$vals['error_count']."\n";
foreach($vals['errors'] as $val){
echo $val['email_address']. " failed\n";
echo "code:".$val['code']."\n";
echo "msg :".$val['message']."\n";
}
}
The above code was cut&paste and modified without testing, as my code is much larger and contains custom schema (i don’t really use memberProfile).. but - it should work.
Good luck.