Get all the lists from Mailchimp account using CURL
In this article, We will get all the lists from a Mailchimp account using API 3.0, for this I'm going to use CURL request. We will use GET request using CURL to get all the lists.
Mailchimp is an email marketing service. It is an American company founded in 2001.
Read: Add new subscriber in mailchimp
API usedapi.mailchimp.com/3.0/lists
To add subscriber in a list you will need mailchimp account API_KEY. I'm using the following function to view all lists from a mailchimp account.
<?php function curlRequest( $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_RETURNTRANSFER, true); curl_setopt($mch, CURLOPT_CUSTOMREQUEST, $request_type); curl_setopt($mch, CURLOPT_TIMEOUT, 10); curl_setopt($mch, CURLOPT_SSL_VERIFYPEER, false); if( $request_type != 'GET' ) { curl_setopt($mch, CURLOPT_POST, true); curl_setopt($mch, CURLOPT_POSTFIELDS, json_encode($data) ); } return curl_exec($mch); } $api_key = 'API KEY HERE'; $data = array('fields' => 'lists'); $url = 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/'; $result = json_decode( curlRequest( $url, 'GET', $api_key, $data) ); if( !empty($result->lists) ) { echo '<ul>'; foreach( $result->lists as $list ){ echo '<li value="' . $list->id . "'>' . $list->name . ' (' . $list->stats->member_count . ')</li>'; } echo '</ul>'; }
?>
In this article, We will get all the lists from a Mailchimp account using API 3.0, for this I'm going to use CURL request. We will use GET request using CURL to get all the lists.
Mailchimp is an email marketing service. It is an American company founded in 2001.
Read: Add new subscriber in mailchimp
API usedapi.mailchimp.com/3.0/lists
To add subscriber in a list you will need mailchimp account API_KEY. I'm using the following function to view all lists from a mailchimp account.
<?php function curlRequest( $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_RETURNTRANSFER, true); curl_setopt($mch, CURLOPT_CUSTOMREQUEST, $request_type); curl_setopt($mch, CURLOPT_TIMEOUT, 10); curl_setopt($mch, CURLOPT_SSL_VERIFYPEER, false); if( $request_type != 'GET' ) { curl_setopt($mch, CURLOPT_POST, true); curl_setopt($mch, CURLOPT_POSTFIELDS, json_encode($data) ); } return curl_exec($mch); } $api_key = 'API KEY HERE'; $data = array('fields' => 'lists'); $url = 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/'; $result = json_decode( curlRequest( $url, 'GET', $api_key, $data) ); if( !empty($result->lists) ) { echo '<ul>'; foreach( $result->lists as $list ){ echo '<li value="' . $list->id . "'>' . $list->name . ' (' . $list->stats->member_count . ')</li>'; } echo '</ul>'; }
?>
Recommanded Articles
- Get Facebook long live access token in AWS Lambda function
- How to make image background transparent using PHP
- Paypal credit card payment on website in PHP
- Capture page screenshot by URL PHP script
- How to get all UK addresses by postal code
- API versioning in Laravel App
- Get all the lists from Mailchimp account using CURL
- Create a LEAD in ZOHO CRM using Node JS
- Create a LEAD in ZOHO CRM using API
- Add new subscriber in a Klaviyo list API
Latest Comments