Created
December 4, 2015 17:35
-
-
Save chadhutchins/1f6632ce4dc63a4ff746 to your computer and use it in GitHub Desktop.
This script shows how to use the MailChimp v3 API to re-subscribe a large group of mistakingly unsubscribed email addresses in a MailChimp List. There are a couple examples commented out that can be used. The first shows how to look up the status of a batch API call sent to MailChimp. And the second shows how to make a batch API call to re-subsc…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// replace this with your mailchimp api key | |
$mailchimp_apikey = "mailchimp-api-key-goes-here"; | |
// create mailchimp api object that is used later to make api calls | |
$mcapi = new MailChimp_API($mailchimp_apikey); | |
/** | |
// Use the following code if you want to look up the status of a batch call | |
// array of batch ids provide after sending batch calls to mailchimp | |
$batch_ids = array( | |
'1478dc8420', | |
'a2f8edac18', | |
'4b46167342', | |
'9c22a6f907', | |
'c5750659c3', | |
'4de8cc1017', | |
'6af75c9a83', | |
'badb3f6ad7', | |
); | |
foreach ($batch_ids as $batch_id) | |
{ | |
$response = $mcapi->get_batches($batch_id); | |
var_dump($response); | |
echo "<hr>"; | |
} | |
exit; | |
*/ | |
/** | |
// Use the following code if you want to mass update subsribers in MailChimp | |
// The example below 'subscribes' people to the MailChimp list even if they have already unsubscribed | |
// This helps if there is an accidental mass unsubscribe | |
// the array of email addresses you want to update | |
$emails = array( | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
); | |
// the mailchimp list id you want to update the emails on | |
$list_id = 'd0ffa58810'; | |
// the size of the batch calls you send at one time | |
$batch_size = 200; | |
$subscribers = array(); | |
$num_emails = count($emails); | |
for ($i=1;$i<=$num_emails;$i++) | |
{ | |
// you can set whatever data you want here to update in MailChimp | |
$subscribers []= array( | |
'id' => uniqid(), | |
'email_address' => $emails[($i-1)], | |
'status' => 'subscribed', | |
); | |
if ($i%$batch_size == 0) | |
{ | |
// send the batch data every one hundred calls | |
$response = $mcapi->update_subscribers($list_id,$subscribers); | |
// print the response | |
var_dump($response); | |
// empty the subscriber array | |
$subscribers = array(); | |
} | |
} | |
// for loop ended, process the final group | |
$response = $mcapi->update_subscribers($list_id,$subscribers); | |
var_dump($response); | |
*/ | |
// helper class to talk with MailChimp v3 batch API | |
class MailChimp_API | |
{ | |
private $apikey = false; | |
function __construct($apikey=false) | |
{ | |
if (!empty($apikey)) | |
{ | |
$this->apikey = $apikey; | |
} | |
} | |
public function update_subscribers($list_id,$subscribers) | |
{ | |
if (empty($list_id)) | |
{ | |
$GLOBALS['log']->warning('MailChimp_API update_subscribers: list_id is empty'); | |
return false; | |
} | |
if (empty($subscribers) or !is_array($subscribers)) | |
{ | |
$GLOBALS['log']->warning('MailChimp_API update_subscribers: subscribers is empty or not an array'); | |
return false; | |
} | |
$operations = array(); | |
$type = "PUT"; | |
$url = "lists/{$list_id}/members/"; | |
foreach ($subscribers as $key => $subscriber) | |
{ | |
if (empty($subscriber['id'])) | |
{ | |
// id is required | |
$GLOBALS['log']->warning('MailChimp_API update_subscribers: no id for subscriber '.print_r($subscriber,true)); | |
continue; | |
} | |
if (empty($subscriber['email_address'])) | |
{ | |
// email address required | |
$GLOBALS['log']->warning('MailChimp_API update_subscribers: no email_address for subscriber '.print_r($subscriber,true)); | |
continue; | |
} | |
$email_hash = $this->get_email_hash($subscriber['email_address']); | |
$path = $url . $email_hash; | |
$operations []= array( | |
"method" => $type, | |
"path" => $path, | |
"operation_id" => $subscriber['id'], | |
"body" => json_encode($subscriber), | |
); | |
} | |
return $this->post_batches($operations); | |
} | |
public function post_batches($operations) | |
{ | |
if (empty($operations) or !is_array($operations)) | |
{ | |
return false; | |
} | |
$type = "POST"; | |
$url = "batches"; | |
$data = array( | |
"operations" => $operations | |
); | |
return $this->call_API($type,$url,$data); | |
} | |
public function get_batches($batch_id) | |
{ | |
if (empty($batch_id)) | |
{ | |
return false; | |
} | |
$type = "GET"; | |
$url = "batches/{$batch_id}"; | |
return $this->call_API($type,$url); | |
} | |
public function get_email_hash($email) | |
{ | |
if (empty($email)) | |
{ | |
$GLOBALS['log']->warning('MailChimp_API get_email_hash: no email provided'); | |
return false; | |
} | |
return md5(strtolower($email)); | |
} | |
// the base API url is auto populated | |
// assuming json is being used goint to MC and returned from them | |
// $ext is used to set the extension | |
// example: call_API('POST', 'lists/' | |
public function call_API($request_type, $ext='', $body='') | |
{ | |
/** | |
// If using in a system where SugarChimp is installed | |
// You can use the following line to get the customers MailChimp apikey | |
$apikey = SugarChimp_Setting::retrieve('apikey'); | |
// otherwise grab it from somewhere else | |
*/ | |
$apikey = $this->apikey; | |
$split = explode('-',$apikey); | |
$dc = $split[1]; | |
$url = $dc . '.api.mailchimp.com/3.0/' . $ext; | |
$curl = curl_init(); | |
$opts = array( | |
CURLOPT_HTTPHEADER => array('Authorization: apikey ' . $apikey), | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_CUSTOMREQUEST => $request_type | |
); | |
switch ($request_type) | |
{ | |
case 'POST': | |
case 'DELETE': | |
$opts[CURLOPT_POST] = 1; | |
$opts[CURLOPT_POSTFIELDS] = json_encode($body); | |
break; | |
case 'PUT': | |
$opts[CURLOPT_POSTFIELDS] = json_encode($body); | |
break; | |
} | |
curl_setopt_array($curl, $opts); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
return json_decode($result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment