Integrate With MailChimp API With This Simple PHP Code

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

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

	$email = "[email protected]"; // SUBCRIBER'S EMAIL
	$apiKey = 'YOUR-API-KEY'; // YOUR API KEY
	$memberId = md5(strtolower($email));
	$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
	$url = 'https://' . $dataCenter . '' . $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;

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

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?