So you want to add subscribers to a Campaign Monitor list from your custom PHP script. Good news is there is a great PHP class available to access their API which very painless to implement in your PHP script and easily accomplishes this task.
Details Required From Campaign Monitor
Firstly, we will need to grab a few details from our Campaign Monitor account in order access the Campaign Monitor API via this script. There are two items we will need, the API Key and List ID. They are a little hidden away, so here is how you find them.
The API Key
- Once your logged into your Campaign Monitor account, go to the Account Settings section, which can be found by clicking on your username icon in the top right hand corner of all pages, it’s the first option.
- On the right hand side of the Account Settings page you should see a few additional options, look for API Keys and click into this page.
- Click the Show API Key link to reveal your the key and copy and paste this somewhere safe for later use.
The List ID
- For the List ID we first need to click on the Lists & Subscribers menu option at the top of page
- Next you will want click into the actual list you would like to add your subscribers to, just click on the list name for this.
- Now this is where things become a little obscure, once you are on the list display page, you need to click change name/type link, which is just below the page title in brackets.
- Now that you have found the edit list page, at the bottom of this page, just above the Save Changes button you will find the API Subscriber List ID section, which will contain your List ID, another long ass code. Like before, copy and paste this somewhere safe for later use.
Now that you have the API Key and List ID, let’s setup the PHP code.
The PHP Code to Add Subscribers
Thanks to the great PHP class CMBase, running this functionality via PHP is super easy, you will first need to download the CMBase.php include, which you can find on Github at the link below.
Once you have this downloaded, simply include it into your PHP script and use the below code to easily add subscribers to your list.
require_once('CMBase.php'); // Details you want to add, could come from $_POST or similar $email = 'firstname.lastname@example.org'; $name = 'Test'; // Campaign Monitor access details $api_key = 'Add Your API Key'; $client_id = null; $campaign_id = null; $list_id = 'Add Your List ID'; // Create API object $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id ); // Send your details to the list $result = $cm->subscriberAdd( $email, $name);
You can include this code in many places, such as in email enquiry forms, custom checkouts, the sky is the limit.
I hope this helps someone with their project 🙂