How to Add Subscribers to a List Using MailChimp REST Api V3

MailChimp REST Api V3 exposes methods that allows you to add, manage members of a specific MailChimp list. The following code snippet help you to add a new subscriber to MailChimp List.

<?php
class ST_Newsletter_Plugin {
	public function __construct(){ 
	
	} 
	
	function subscribe($user_id, $user_email){
		//get api_key and list id from admin settings
		$api_key = "c8d8c17ae54aui8ujnj43343-us9";
		$list_id = "2d9e113i8hm";
		
		require_once('MailChimp.php');
		$mailChimp = new MailChimp($api_key);
		
		$result = $mailChimp->post("lists/$list_id/members", [
			'email_address' => $user_email,
			'status'=> 'subscribed'
		]);

		if ($mailChimp->success()) {
		    echo "success";
		} else {
		   echo "fail";
		}
	}
}

?>

You can download “MailChimp.php” from GitHub.

Tagged in : #PHP

Stacktips

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages. Follow us on Twitter

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.