MailChimp API 3.0 – Get All List Members

It is ok if there are not so much members in your lists. But what to do if your list contains 10000 members or more?

#httpapi, #mailchimp, #MailChimp  /  April 12  /   18

In MailChimp API docs it said:

Use offset and count in the URL query string to paginate because it provides greater control over how you view your data.

Offset defaults to 0. Count defaults to 10.

So, it means if you change count you can increase the number of results. Like this

https://usX.api.mailchimp.com/3.0/lists/{LIST_ID}/members?count=20

Seems simple but the maximum allowed count parameter is 50. So, you can not get more than 50 subscribers just in a one API call.

OK, what to do if you have much more subscribers in a list?

What if your MailChimp list consists of thouthands members.

Just use both count and offset parameters! For example if you want to get 100 subscribers, you will need 2 API calls:

https://usX.api.mailchimp.com/3.0/lists/{LIST_ID}/members?offset=0&count=50

and

https://usX.api.mailchimp.com/3.0/lists/{LIST_ID}/members?offset=50&count=50

Use the example below to get thouthands of your list members at once

If you would like to use WordPress example, please scroll down a little, if you connect to MailChimp API via cURL, then copy this function and insert it somewhere in your website files. You haven’t to change anything in it, just copy it as is.

So, now our MailChimp API connection becomes easier.

  • If you do not know where to get API key, read this.
  • If you can not find your list ID, read this.
$api_key = 'YOU API KEY';
$list_id = 'LIST ID';
$dc = substr($api_key,strpos($api_key,'-')+1); // us5, us8 etc
 
// URL to connect
$url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id;
 
// connect and get results
$body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key ) );
 
// number of members in this list
$member_count = $body->stats->member_count;
$emails = array();
 
for( $offset = 0; $offset < $member_count; $offset += 50 ) :
 
	$data = array(
		'offset' => $offset,
		'count'  => 50
	);
 
	// URL to connect
	$url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members';
 
	// connect and get results
	$body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key, $data ) );
 
 	foreach ( $body->members as $member ) {
		$emails[] = $member->email_address;
	}
 
endfor;
 
print_r( $emails );

WordPress example

Actually for WordPress you can use any of these examples. Both of them should work well.

$api_key = 'YOU API KEY';
$list_id = 'LIST ID';
$dc = substr($api_key,strpos($api_key,'-')+1); // us5, us8 etc
$args = array(
 	'headers' => array(
		'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
	)
);
 
// connect
$response = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id, $args );
 
// decode the response
$body = json_decode( $response['body'] );
 
if ( $response['response']['code'] == 200 ) :
 
	// subscribers count
	$member_count = $body->stats->member_count;
	$emails = array();
 
	for( $offset = 0; $offset < $member_count; $offset += 50 ) :
 
		$response = wp_remote_get( 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members?offset=' . $offset . '&count=50', $args );
		// decode the result
		$body = json_decode( $response['body'] );
 
		if ( $response['response']['code'] == 200 ) {
			foreach ( $body->members as $member ) {
				$emails[] = $member->email_address;
			}
		}
 
	endfor;
 
endif;
 
// print all emails
print_r( $emails );

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 18

  • Craig R Morton January 17, 2017 at 08:53

    Thanks for posting this. I am not using WordPress, but your first code snippet gave me the idea on how to tackle this in my SF2 application.

  • Cormac Champion February 7, 2017 at 01:21

    How would I extract details of Merge Fields ? Should I change email_address for merge1 ? And o extract multiple merge fields, do I separate them with commas ?

  • Cormac Champion February 7, 2017 at 13:30

    Sorry, I mean I want to extract details from CERTAIN Merge Fields (not all) but I do NOT want to extract the email address. So I was thinking I might need to change something like this

    $emails[] = $member->merge1, merge3, merge4;

    ???

    • MishaAuthor February 7, 2017 at 15:37

      If I understand you properly:

      $emails[] = $member->merge_fields->merge1;

      for multiple fields:

      $emails[] = $member->merge_fields;

      I mean in my previous comment that you can find out everything by yourself using print_r() function.

  • Cormac Champion February 7, 2017 at 20:32

    Almost there

    I have 5 merge fields. I want to ONLY extract merge1, merge3 and merge4 – so just a selection of all merge fields

    So what would the delimiter be between fields – like this ?

    $emails[] = $member->merge_fields->merge1,merge3,merge4;

    • MishaAuthor February 8, 2017 at 16:12

      I understand you,

      here it is:

      $emails[] = $member->merge_fields->merge1 . ' ' . $member->merge_fields->merge3 . ' ' . $member->merge_fields->merge4;
  • thx. this post is vary useful

  • This seems to stop functioning after it hits a certain limit

    • MishaAuthor June 10, 2017 at 11:34

      What is your limit, Jon?

      • Thank you for the very fast reply.

        My apologies. That was a premature comment, I should have investigated further first. I am getting 2,300+ subscribers returned. What is strange, however, is that some users who are in the list on MailChimp’s backend do not get returned in this list. That’s where I thought it was a hitting a limit, but the count was just a coincidence. I’ve checked the list ID (there’s only one), I’ve checked to make sure the user is subscribed to the list, made sure their status is ‘subscribed’…all checks out. For some reason, however, their email does not get returned in the array. It only happens for some, not all.

        • MishaAuthor June 12, 2017 at 09:21

          Strange. I remember that I printed 8K subscribers. But maybe not all as well – I didn’t checked. I will try to test this issue when I have a time.

          Thank you.

  • Arpit Mittal July 6, 2017 at 15:34

    Hi Misha,

    I’m trying to make use mailchimp API to work on my custom CMS, i would like show the stats, subscribed users, campaign stats, campaign templates etc I nearly want to retrieve and show everything on the admin dashboard. So that admin doesn’t need to login to the Mailchimp.

    Can you please guide me or can you provide me resources? or give some useful link so that i can get started and learn few basic and advance things to use Mailchimp API.

    Thank You

    • MishaAuthor July 7, 2017 at 21:43

      Hi,

      Sure, this helped me a lot https://developer.mailchimp.com/documentation/mailchimp/reference/overview/

  • Patrick Strietzel July 8, 2017 at 02:12

    Hey Misha,
    thank so so much for this tutorial – I’ve been looking for something like this for ages and
    I always felt quite overwhelmed by the mailchimp API.

    In my case I was looking for a lighweight method to display a table of subscribers in wordpress for logged in editors without the need of switching to mailchimp admin area.

    I’m heavily using custom merge fields and I was quite delighted you can easily output them by their custom merge name e.g. ‘$member->merge_fields->ADDRESS’.

    Maybe this snippet is useful for someone:

    <?php
    include ('inc/mailchimp_curl_connect.php'); // include curl connect function: https://rudrastyh.com/mailchimp-api/get-lists.html#mailchimp_api_connect
    $api_key = 'XXXX';
    $list_id = 'XXXX'; 
    ?>
     
    <table>
    	<thead>
    	   <tr>
    		   <th>First Name</th>
    		   <th>Last Name</th>
    		   <th>E-Mail</th>
    		   <th>Address</th>
    		   <th>changed</th>
    	   </tr>
    	</thead>
    	<tbody>
    <?php
     
    $dc = substr($api_key,strpos($api_key,'-')+1); // us5, us8 etc
    // URL to connect
    $url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id;
    // connect and get results
    $body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key ) );
    // number of members in this list
    $member_count = $body->stats->member_count;
    $emails = array();
     
    for( $offset = 0; $offset < $member_count; $offset += 50 ) :
     
    	$data = array(
    		'offset' => $offset,
    		'count'  => 50
    	);
     
    	// URL to connect
    	$url = 'https://'.$dc.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members';
     
    	// connect and get results
    	$body = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key, $data ) );
     
     	foreach ( $body->members as $member ) {
    		echo '
    		<tr>		
    			<td class="member_FNAME">'.$member->merge_fields->FNAME.'</td> 
    			<td class="member_LNAME">'.$member->merge_fields->LNAME.'</td>
    			<td class="member_email">'.$member->email_address.'</td>
    			<td class="member_LNAME">'.$member->merge_fields->ADDRESS.'</td>
    			<td class="member_BEGLNAM">'.$member->last_changed.'</td>
    		</tr>';
    	}
    endfor;
     ?>
    	</tbody>        
    </table>
  • Hi Im having Fatal error: Call to undefined function rudr_mailchimp_curl_connect()

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email