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.
<?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); ?>