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  /  October 11, 2016  /   8

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

once a week, no spam

Comments 8

  • Craig R MortonJanuary 17, 2017 at 08:01

    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 ChampionFebruary 7, 2017 at 01:02

    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 ChampionFebruary 7, 2017 at 13:02

    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;

    ???

    • 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 ChampionFebruary 7, 2017 at 20:02

    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;

    • 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

Leave your question or feedback

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