Search the blog

MailChimp’s API only lets you get so many list items at a time. This PHP sample shows you how to get a list regardless of its size. To do this you will need a simple wrapper by DrewM, available for free from GitHub. Per the link, you can install it via Composer or simply download and include the class file.

Once the wrapper is available here’s how you do it.

// Max number you can get at one time
$count  = 50;

// Settings
$mailChimpAPIKey = 'YOUR-MAILCHIMP-API-KEY';
$listID = 'YOUR-LIST-ID';

$mailChimp = new MailChimp($mailChimpAPIKey);

// Gets the number of items
$list = $mailChimp->get('lists/' . $listID . '/members', [

    'fields' => 'total_items'

]);

// Work out the number of iterations
$iterations = ceil($list['total_items'] / $count);

$list = [];

// Get $count at a time and merge into a single array
for ($counter = 1; $counter <= $iterations; $counter ++) {

    $array = $mailChimp->get('lists/' . $listID . '/members', [

        'offset'    => (($counter - 1) * $count),
        'count'     => $count

    ])['members'];
    
    $list = array_merge($list, $array);

}

// $list now has the entire list
Tim Bennett is a freelance web designer from Leeds. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.