Skip to content

Instantly share code, notes, and snippets.

@gimesi
Created September 11, 2013 10:12
Show Gist options
  • Save gimesi/6521712 to your computer and use it in GitHub Desktop.
Save gimesi/6521712 to your computer and use it in GitHub Desktop.
Show the number of subscribers of a Mailchimp Newsletter.
<?php
/* Show the number of subscribers of a Mailchimp Newsletter
*
* Contains "Mini MailChimp API v2 wrapper" by Drew McLellan
* to be found at https://github.com/drewm/mailchimp-api
*/
class MailChimp
{
private $api_key;
private $api_endpoint = 'https://<dc>.api.mailchimp.com/2.0/';
function __construct($api_key)
{
$this->api_key = $api_key;
list(, $datacentre) = explode('-', $api_key);
$this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint);
}
public function call($method, $args=array())
{
return $this->_raw_request($method, $args);
}
private function _raw_request($method, $args=array())
{
$args['apikey'] = $this->api_key;
$url = $this->api_endpoint.'/'.$method.'.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
$result = curl_exec($ch);
curl_close($ch);
return $result ? json_decode($result, true) : false;
}
}
$MailChimp = new MailChimp('YOURAPIKEY'); // enter your API-Key here
$member_count = $MailChimp->call('lists/list');
?>
<?php echo $member_count['data'][0]['stats']['member_count']; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment