How to get your MailChimp Subscribers count with PHP and MailChimp API?

Updated on September 1, 2017

Do you use MailChimp to get visitors subscribed to your newsletter? Then you may also want to display subscribers count on your website (may be just above/below the subscription box), so that the visitor is aware of the newsletter’s popularity and encourages him/her to subscribe to your content. But how? Fortunately, MailChimp provides an API that allows you to access most of the MailChimp features from any CMS, shopping cart or a blog.

I did some Googling to get this done and found an useful article by Louis@impressiveweb – Below are the steps to pull subscriber count using MailChimp API with PHP.

Step 1: Download MailChimp API Source.

Head on to this link and download the PHP API source code.

Step 2: Register for API Key

In order to use MailChimp API, you need to obtain a key. To do that, login to your MailChimp account > Click on the Profile name > Account > Extras and choose API keys.

Step 3: Include API in your PHP code

include "Mailchimp.php";

Step 4: Use the API key

Copy and paste the API key (obtained in Step 2 ) in the below link to create a MailChimp object.

$MailChimp = new MailChimp('your_api_key_goes_here');

Step 5: Call the list method

Once you have an instantiated object, make a call to list method. The list method is accessible to API users with the roles author, admin and manager.

$mc = $MailChimp->call('lists/list');

Step 6: Get the subscriber count

$subscriber_count = $mc[data][0][stats][member_count];
echo number_format($subscriber_count);

Well, the complete code is below:

include "Mailchimp.php";
$MailChimp = new MailChimp('your_api_key_goes_here');
$mc = $MailChimp->call('lists/list');
$subscriber_count = $mc[data][0][stats][member_count];
echo number_format($subscriber_count);

subscription count

But wait! MailChimp API has limitations…

MailChimp API has a connection limit – the API users are allowed to make 10 simultaneous connections and if the limit is reached, an error message is thrown. Howover, you may not want to pull the subscriber count on every page refresh (you know the reason, it’s not the better way ). So the ideal way is to pull the data once per day and cache it. To do that, use the below code.

include "Mailchimp.php";
$lastRunLog = 'logs/lastrun.log';
$subfile = 'logs/subcount.log';
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 86400) {
$MailChimp = new MailChimp('your_api_key_goes_here');
$mc = $MailChimp->call('lists/list');
$subscriber_count = $mc[data][0][stats][member_count];
file_put_contents($lastRunLog, time());
file_put_contents($subfile, $subscriber_count);
} else {
$subscriber_count = file_get_contents($subfile);
echo number_format($subscriber_count);

To explain it briefly, the API pulls the count and stores it in a file named ‘subcount.log’. The file ‘lastrun.log’ keeps track of the time when the count was pulled from MailChimp. Now, all you need to do is check whether its been 24 hours since the last API call was made. If yes, pull the new count, else read the count from file. Here’s a better explanation.

Alternatively, you may also run the api code via cron and store the data in a file or a database.

Was this article helpful?

Related Articles

Leave a Comment