Add a Mailchimp Subscriber with PHP

You may want to add a subscriber to a Mailchimp list, but can’t or don’t want to use one of their pre-made code snippets to plug into your site. One popular reason is to avoid the confirmation email that Mailchimp will send. Others may just want to merge subscribe functionality in with a larger form. Regardless of the reason we are going to go over how to do this using a combination of PHP, MailChimp API v3.0, and cURL.

Function to add a Mailchimp subscriber

This is the php function we use to add the subscriber to mailchimp that is using cURL. This is good as-is and you can just stick it in any php file where it can be called later as needed. If you are using WordPress for example, this could be stuck right into the functions.php file of your active theme.

<?php
function vcmc_subscribe($email, $fname, $lname, $apikey, $listid, $server) {
    $auth = base64_encode( 'user:'.$apikey );
    $data = array(
        'apikey'        => $apikey,
        'email_address' => $email,
        'status'        => 'subscribed',
        'merge_fields'  => array(
            'FNAME' => $fname,
            'LNAME' => $lname
        )
    );
    $json_data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$listid.'/members/');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$auth));
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    $result = curl_exec($ch);
};
?>

Grab the Data and Call Our New Function

Now we set all of our needed values that we are going to pass into the function we made. I’m using $_POST below, but you could use $_GET as well or any other method that gets you the values. Once those are set, we just call our new function and pass the values in. That’s it!

<?php
$email = $_POST["youremail"];
$fname = $_POST["firstname"];
$lname = $_POST["lastname"];
$apikey = 'YOUR MAILCHIMP ACCOUNT API KEY';
$listid = 'YOUR MAILCHIMP LIST ID';
$server = 'YOUR MAILCHIMP ACCOUNT SERVER'; //example 'us1.'
vcmc_subscribe($email, $fname, $lname, $apikey, $listid, $server);
?>

$email, $fname, and $lname values would generally be the ones being passed through from a user filling out a form.
$apikey, $listid, and $server values can be found within your mailchimp account and will need to be entered manually in the code above. I’ll show you now where to find those values in case you haven’t done it before.

API Key

To find your API Key, first go to your name in the upper right and click to open the dropdown menu. Within that menu click on ‘Account’ and you’ll be brought to the page you see below.

Mailchimp Extras
Now you can click on ‘Extras’ and select ‘API keys’ as shown. You’ll either already have an API key setup that you can use or you can create a new one by clicking on the ‘Create A Key’ button at the bottom. That’s the first value you will need.

Mailchimp API Key

List ID

We will also need to grab the List ID for the mailing list that you will want to be subscribing the users to. So in the ‘Lists’ section of Mailchimp, you will want to click on the arrow to the right of your desired list and select ‘Settings’ from the dropdown menu that appears. Of course, if you have no lists setup already, you will have to create one using the ‘Create List’ button.

Mailchimp List Settings
Now that you are in the settings of your list, you just need to scroll to the bottom and you will see the Unique ID for this specific list. That is the second value you will need.

Mailchimp List ID

Server

The last value you need can just be seen in the URL when you are logged into mailchimp. It will look something like this: https://us1.admin.mailchimp.com
From that URL we need the text between https:// and admin.mailchimp.com. So in this example we would be setting our $server value to ‘us1.’
IMPORTANT: Do not forget the period at the end of this value!

Those are all the mailchimp values you need and now you can just plug them into the provided code snippet where noted.

Speak Your Mind

*