Mailchimp API, batchSubscribe example
<?php
require_once 'Mailchimp.php';
//Requirements for connecting to mailchimp
$url_endpoint_template = 'https://{{dc}}.api.mailchimp.com/2.0/';
$api_key = 'api-key';
//Findout the endpoint url
$url_endpoint = str_replace('{{dc}}', substr($api_key, -3), $url_endpoint_template);
$mailchimp = new Mailchimp($api_key);
$mylist = $mailchimp->lists->getList(array('list_name' => 'My Contacts'));
//Returns an array with lists info and a data component list['data'] witch is an array with lists and info about them
//Getting only the ids
$ids = getListIds($mylist);
//Create Batch
$batch = array(
array(
'email' => array('email'=> 'zaxarovounis@infogeek.gr'),
'email_type' => 'html',
'merge_vars' => array('fname' => 'lalas', 'lname' => 'piou')
)
);
/**
* Subscribe two email address
* @param list_id
* @param batch (above)
* @param send_confirmation_email boolean (if false it subscribes the email address directly)
* @param update_existing boolean
* @param replace_interests boolean
*/
$result = $mailchimp->lists->batchSubscribe($ids[0], $batch, false, true, true);
//Function for getting only the ids
function getListIds($mailchimp_list){
$ids = array();
foreach ($mailchimp_list['data'] as $list){
array_push($ids, $list['id']);
}
return $ids;
}