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?

October 11, 2016 / #HTTP API, #MailChimp

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 1

  • 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.

Leave your question or feedback

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