Integrating your website with MailChimp is easily done with this code.

You can connect it with any form that’s processed with PHP.

<?php
	$email = "[email protected]"; // SUBCRIBER'S EMAIL
	$apiKey = 'YOUR-API-KEY'; // YOUR API KEY
	$listId = 'YOUR-LIST-ID'; // YOUR LIST ID ON MAILCHIMP
	
	$memberId = md5(strtolower($email));
	$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
	$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;

	$json = json_encode([
		'email_address' => $email,
		'status'        => 'subscribed' // Could be "subscribed"/"unsubscribed"/"cleaned"/"pending"
	]);

	$ch = curl_init($url);

	curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
	curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                                                                                 

	$result = curl_exec($ch);
	echo $result;
	curl_close($ch);
?>

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?