FileMaker / MailChimp integration with SmartPill's PHP plugin

Using the MailChimp PHP API and the FIlemaker plugin SmartPill, which enables you to execute php from within a FIlemaker database, I was able to syncronize data between a FM database and a MailChimp list.

Here is a script that takes a found set and uploads the data to MailChimp. The MailChimp API automatically adds new records and matches on old ones.
''//contains apikey

$api = new MCAPI($apikey);

$batch = array();

$foundCount =  fm_evaluate('Get(FoundCount)');

for (
$i 1$i <= $foundCount$i++) {

$fname fm_evaluate('GetNthRecord(FNAME;'.$i.')');
$lname fm_evaluate('GetNthRecord(LNAME;'.$i.')');
$email fm_evaluate('GetNthRecord(EMAIL;'.$i.')');
$groups fm_evaluate('GetNthRecord(Groups;'.$i.')');

$batch[] = array('EMAIL'=>$email'FNAME'=>$fname'LNAME'=>$lname'INTERESTS'=>$groups);

$optin false//no, don't send optin emails
$up_exist true//yes, update currently subscribed users
$replace_int true//add interests, don't replace

$vals $api->listBatchSubscribe($listId$batch$optin$up_exist$replace_int);

if (
$api->errorCode) {
"Batch Subscribe failed!\n";
"msg :".$api->errorMessage."\n";

else {
"added:   ".$vals['add_count']."\n";
"updated: ".$vals['update_count']."\n";
"errors:  ".$vals['error_count']."\n";
$vals['errors'] as $val) {
$val['email_address']. " failed\n";
"msg :".$val['message']."\n";

© 2011 Adam Gerson.