MailChimp API v3.0 Get Lists in PHP

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.mailchimp.com/3.0/lists/. API prefix is the last part of your API key, for example us8, us10 etc.

$api_key
(string) Do not know where to obtain API key? Check this small guide.
$data
(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 http://developer.mailchimp.com/documentation/mailchimp/reference/lists/
$data = array(
	'fields' => 'lists', // total_items, _links
	//'email' => 'misha@rudrastyh.com',
	'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) . '.api.mailchimp.com/3.0/lists/';
$result = json_decode( rudr_mailchimp_curl_connect( $url, 'GET', $api_key, $data) );
//print_r( $result);

if( !empty($result->lists) ) {
	echo '';
} elseif ( is_int( $result->status ) ) { // full error glossary is here http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
	echo '' . $result->title . ': ' . $result->detail;
}
MailChimp API - get Lists
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter