MailChimp API v3.0 Get Lists in PHP

You could also get only lists that include a specific subscriber’s email address, or get the lists, created in between of specific time period.

June 7, 2016 / #APIs, #MailChimp

First of all — when do you need to get the information about MailChimp lists in account? It is not such a popular function like subscribing and unsubscrubing users but it is also very useful.

For example it lays in a base of my plugin which allows to synchronize the subscribers from a specific list with your WordPress website users (any role or membership plan).

MailChimp API cURL connection function for any purposes #

No matter what task stands in front of you, the MailChimp cURL connection always looks the same. Only parameters are different. And of course your server should support cURL.

From now I will refer to this function from another posts about MailChimp API.

function rudr_mailchimp_curl_connect( $url, $request_type, $api_key, $data = array() ) {
	if( $request_type == 'GET' )
		$url .= '?' . http_build_query($data);
	$mch = curl_init();
	$headers = array(
		'Content-Type: application/json',
		'Authorization: Basic '.base64_encode( 'user:'. $api_key )
	curl_setopt($mch, CURLOPT_URL, $url );
	curl_setopt($mch, CURLOPT_HTTPHEADER, $headers);
	//curl_setopt($mch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
	curl_setopt($mch, CURLOPT_RETURNTRANSFER, true); // do not echo the result, write it into variable
	curl_setopt($mch, CURLOPT_CUSTOMREQUEST, $request_type); // according to MailChimp API: POST/GET/PATCH/PUT/DELETE
	curl_setopt($mch, CURLOPT_TIMEOUT, 10);
	curl_setopt($mch, CURLOPT_SSL_VERIFYPEER, false); // certificate verification for TLS/SSL connection
	if( $request_type != 'GET' ) {
		curl_setopt($mch, CURLOPT_POST, true);
		curl_setopt($mch, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json
	return curl_exec($mch);
$url, $request_type
(string) You can find both $url and $request_type in MailChimp API reference easily.

Where to get URL and request type for API connection

So, the request type is GET and the URL is https://{API PREFIX} API prefix is the last part of your API key, for example us8, us10 etc.

(string) Do not know where to obtain API key? Check this small guide.
(array) This is the associative array of parameters to send to MailChimp API, it supports both query string parameters (GET) and request body parameters (POST, PATCH, PUT).

Get your lists. How to get only lists that include a specific subscriber’s email

$api_key = 'YOUR API KEY HERE';
// Query String Perameters are here
// for more reference please vizit
$data = array(
	'fields' => 'lists', // total_items, _links
	//'email' => '',
	'count' => 5, // the number of lists to return, default - all
	'before_date_created' => '2016-01-01 10:30:50', // only lists created before this date
	'after_date_created' => '2014-02-05' // only lists created after this date
$url = 'https://' . substr($api_key,strpos($api_key,'-')+1) . '';
$result = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key, $data) );
//print_r( $result);
if( !empty($result->lists) ) {
	echo '<select>';
	foreach( $result->lists as $list ){
		echo '<option value="' . $list->id . '">' . $list->name . ' (' . $list->stats->member_count . ')</option>';
		// you can also use $list->date_created, $list->stats->unsubscribe_count, $list->stats->cleaned_count or vizit MailChimp API Reference for more parameters (link is above)
	echo '</select>';
} elseif ( is_int( $result->status ) ) { // full error glossary is here
	echo '<strong>' . $result->title . ':</strong> ' . $result->detail;
  • Uncomment line #7 to restrict the results only to lists which include the subscriber with a specific email.
    	//'email' => '',
  • If you want to look at the error reporting in action — just try to use incorrect MailChimp API.
  • Comment line #6 and use print_r( $result); at line #15 to view the full API response.
MailChimp API - get Lists

Only the best of WordPress

once a week, no spam

Comments 2

Leave your question or feedback

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