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.
<?php>
require_once 
'MCAPI.class.php';
require_once 
'config.inc.php'//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) {
  echo 
"Batch Subscribe failed!\n";
  echo 
"code:".$api->errorCode."\n";
  echo 
"msg :".$api->errorMessage."\n";

else {
  echo 
"added:   ".$vals['add_count']."\n";
  echo 
"updated: ".$vals['update_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";
  }
  echo 
print_r($batchtrue);

?>
© 2011 Adam Gerson.