Get all the lists from Mailchimp account using CURL

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 used
api.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.

Also Read: How to upload multiple images or files from frontend in WordPress without any plugin
<?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>';
}
?>

Also Read: Create a LEAD in ZOHO CRM using Node JS

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 used
api.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.

Also Read: How to upload multiple images or files from frontend in WordPress without any plugin
<?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>';
}
?>

Also Read: Create a LEAD in ZOHO CRM using Node JS

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment

Close