MailChimp API – Subscribe User Through PHP

MailChimp API - Subscribe User Through PHP

June 30, 2017

MailChimp API

This ia a Mailchimp API example using PHP and cURL.
This will helps you add more subscribers to your MailChimp lists using PHP and cURL Method.

So MailChimp let your users to susbscribe and you can send them newsletters in bulk and manage your subscriber’s lists.
Things which are required before you start code.
Sign Up on MailChimp

After successfull account creation and activation, Generate your API keys and List ID.

Go to your Account Settings -> Extras -> API Keys
Click on Create a Key button and you will have you API key ready.
After that go to Lists section, Create List, Visit your created lists under Settings -> List name & defaults you will find List ID.

Based on the List Members Instance docs, the easiest way is to use a PUT request which according to the docs either “adds a new list member or updates the member if the email already exists on the list”.

PHP CODE

MailChimp API send request through CURL so make sure you have enabled CURL in your PHP.

Using this curl method auto-subscribe a user without confirming activation URL directly.

I’m using the following function to add and update list members. You may need to include a slightly different set of merge_fields depending on your list parameters.

$data = [
    'email'     => '[email protected]',
    'status'    => 'subscribed',
    'firstname' => 'john',
    'lastname'  => 'doe'
];

subscribeMailchimp($data);

function subscribeMailchimp($data) {
    $apiKey = 'your api key';
    $listId = 'your list id';

    $memberId = md5(strtolower($data['email']));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;

    $json = json_encode([
        'email_address' => $data['email'],
        'status'        => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
        'merge_fields'  => [
            'FNAME'     => $data['firstname'],
            'LNAME'     => $data['lastname']
        ]
    ]);

    $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);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $httpCode;
}

If you have any query or need any further explanation please do not hesitate to reply me.

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

Tags

API IntegrationNewsletter SignupWeb Development

Related Posts

Many new updates happened for Android developers lately after Google I/O. Initially there was no restriction on some features but now.
phpMyAdmin is a web-based database management tool that you can use to view and edit the MySQL databases on your EC2 instance.
Title attribute can be helpful to expand on the meaning of your navigation label and give your users more context You can provide to the link.