How To Integrate Mailchimp Using PHP CURL




In this comprehensive guide, I will meticulously illustrate and elucidate the process of integrating Mailchimp into your PHP application. Through clear, step-by-step instructions and detailed explanations, you will gain a profound understanding of how to seamlessly incorporate Mailchimp functionality, ensuring an effortless and efficient implementation process.


Mailchimp provides a robust API (Application Programming Interface) that allows developers to programmatically interact with their email marketing platform. Understanding the Mailchimp API is crucial for integrating Mailchimp into your PHP application effectively.

Mailchimp API provides various endpoints for managing different aspects of your account, such as lists, subscribers, campaigns, templates, etc

Settings Up Mailchimp Account

Step-by-step guide on creating a Mailchimp account if not already done. checkout this video




How to insert An Email in Mailchimp :

<?php
    $email = 'EMAIL_ADDRESS';
    $list_id = 'LIST_ID';
    $api_key = 'API_KEY';
    $data_center = substr($api_key,strpos($api_key,'-')+1);
    $url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members';
    $jsonData = json_encode([
        'email_address' => $email,
        'status'        => 'subscribed', //pass 'subscribed' or 'pending'
    ]);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
    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_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    $result = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    print_r($result);
  ?>



Make sure to replace 'API_KEY', 'LIST_ID', and other placeholders with your actual Mailchimp API key, list ID, and subscriber information.

This code sends a POST request to the Mailchimp API endpoint to add a subscriber to the specified list. The response will indicate whether the subscriber was added successfully or if there was an error.



Delete An Email From Mailchimp :
 


To delete an email from a Mailchimp list using PHP cURL, you would need the email address of the subscriber you want to delete and the list ID from which you want to remove the subscriber. Below is an example code snippet to delete a subscriber from a Mailchimp list

<?php
    $email = 'EMAIL';
    $list_id = 'LIST _ID';
    $api_key = 'API_KEY';
    $data_center = substr($api_key,strpos($api_key,'-')+1);
    $url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members/'. md5(strtolower($email));
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    print_r($result);
?>



Test your integration thoroughly, especially if you're dealing with sensitive user data. Once tested, deploy your PHP application with Mailchimp integration to your production environment.


That's a basic overview of integrating Mailchimp with PHP using their official SDK. Make sure to refer to the Mailchimp API documentation for more detailed information and advanced features.

Previous Post Next Post

Contact Form