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.
So, the request type is
GET
and the URL ishttps://{API PREFIX}.api.mailchimp.com/3.0/lists/
. API prefix is the last part of your API key, for exampleus8
,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;
}
- Uncomment line #7 to restrict the results only to lists which include the subscriber with a specific email.
//'email' => 'misha@rudrastyh.com',
- 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.

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
Excellent! You just made my life easier :D
Wow! It’s Good!
Your code is useful for me!
Thanks!
Thank you for this! I have a question, though: how can I sort the lists by name? They seem to be sorted by creation date by default.
Hi,
first of all add somewhere this function:
by the second step, before you print
$result->lists
, you have to sort it.I think you can add the second piece of code just after this
if( !empty($result->lists) ) {
Thanks! :)
Welcome ;)
Thanks so much for this! It works fantastic on GETs, but on DELETE, it returns
stdClass Object ( [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ [title] => Method Not Allowed [status] => 405 [detail] => The requested method and resource are not compatible. See the Allow header for this resource’s available methods. [instance] => )
I sent back the same $body object I got from the GET (after inserting the subscribers into my own database). Any ideas how to get around this?
Thanks very much!
Hi Karen,
is your code looks like this?
DELETE method returns nothing for me, but it works as expected.
You rock! The md5( $email ) was where I went wrong. Thank you so much!! <3
Welcome! I’m glad to help :)
This is fantastic.
I’m building a subscribe/unsubscribe tool. I wanted to display all the lists as a checkbox, and have the lists the user is subscribed to checked.
I’m currently creating two queries. One to get all lists and another for lists filtered by the email address. Any ideas if this is possible with a single query?
Cheers
Hi Elliot,
Hmm.. Not sure.
The only idea is to use WordPress Transients to cache the result.
Thanks – Just looped through and created an array of subscribed IDs in the end. Appreciate these articles. Very helpful.
You’re welcome! I’m glad to help.
This is enormously useful, however I want to get campaigns not lists (for one Listid), however I can use the GET/campaign-search but I am struggling to find the format for the query string to tell it to search for only a specific listid.
Got any ideas?
Hi Brian,
did you try this?
I get
“400: Please provide ‘query’ parameter in the query string to search”
So I tried
and it returned an empty set (but no error message)
Brian,
I’ve just tried the example from my previous comment and everything works perfectly.
aha – that is because you are using “campaigns” not “campaign-search”. Ok, as you say that works now – thanks.
However there are a lot more campaigns than that for this list, but it is only returning 10 of them. I tried changing the timeout (which I suspect is ony 10 by co-incidence). How can I get all of them?
Found the “count”=>”999” parameter. This works fine now.
Thanks for your help.
Ok, great :)
Hello, How can send array field type for example currently I have this but not found. please help me. thanks.
Hello Joseph,
Sorry, I do not understand what you mean. Please describe your question more correctly.
Hi, How can send simple html email using mailchimp in php.
Thanks in Advance.!
Hi,
I will add it to my tutorial ideas.
Hello friend, I have a question and need your help.
Why do I need to refresh my MAILCHIMP account page(let it be any page) after I have added a member in existing list through PHP.
In my form I have a Dropdown which shows existing members and its count(like yours) and when I receive status code 200 after adding the member to the list, the dropdown count remains unchanged until I go to the mailchimp account and refresh any page and comeback to form and refresh it.
I don’t know why it is happening. Please help me out.
Hi,
If you don’t refresh the page in mailchimp account, the member will be added to the list anyway
Thank you sir your reply.
But sir, I am using your above code to fetch all LISTS and their MEMBER count.
Immediately after saving the member in the list, when I come back to the dropdown, I am seeing the old count, not the new one.
But as I am refreshing the Mailchimp page and then returning to dropdown form and refreshing it, the count gets updated.
Here’s the code which get the lists.
Hmm, maybe it is some kind of cache. I never faced with this issue.
Can you try it for me sir.?
So that I can know whether I am wrong or there’s some problem with the Mailchimp?
I never faced with this issue.
No problem sir. Solved the issue.
Sir, can you tell how can we send our own message with the mailchimp template while sending an email.?
Hi Misha. When I send curl request to get the lists the response is an empty array.
Do you suppose why?
oh sorry, not an array, but nothing))
Try to print your
$access_key
before the function, I’m not sure you’re passing it the correct way.