Buffer

Truelogic Blog

Notes from the world of software development

RSS 2.0

Integrate the Mailchimp API with your PHP application

MailChimp
  • Sharebar
MailChimp

MailChimp

WHAT IS MAILCHIMP

For those unfamiliar with Mailchimp it is an online service for handling subscribers and newsletters. It is mainly used to handle subscription lists and sending newsletters via its Mailchimp API which has wrappers and integration features for various languages/platforms:

  • .NET
  • PHP
  • Actionscript
  • GRails
  • Java
  • Lasso
  • node.js
  • Objective-C
  • Perl
  • Python
  • Ruby on Rails

The API docs can be found here

In this article we will take a quick look at how to integrate this with your PHP application. If you want a detailed reference on integration then its better to look at the official API documentation. Here, we just want to give a simple and practical explanation on how to go about it.

INTEGRATION ASPECTS

At the very basic level, integration deals with only two things:

  1. Subscribers
  2. Newsletters

HANDLE SUBSCRIBERS

First get your API Key. To do this you need to signup. Check this URL

Once you have obtained the API key, put in a config file which can be included in your page. eg. config.inc.php

    //API Key - see http://admin.mailchimp.com/account/api
    $apikey = 'YOUR MAILCHIMP APIKEY';
    
    // A List Id to run examples against. use lists() to view all
    // Also, login to MC account, go to List, then List Tools, and look for the List ID entry
    $listId = 'YOUR MAILCHIMP LIST ID - see lists() method';
    
    // A Campaign Id to run examples against. use campaigns() to view all
    $campaignId = 'YOUR MAILCHIMP CAMPAIGN ID - see campaigns() method';

    //some email addresses used in the examples:
    $my_email = 'INVALID@example.org';
    $boss_man_email = 'INVALID@example.com';

    //just used in xml-rpc examples
    $apiUrl = 'http://api.mailchimp.com/1.3/';

Design your HTML form. At the very least you will need an email id field to capture. Apart from this you can use Merge Tags to add into your code.

If you want to implement the code using AJAX/Javascript then you need to plan the callbacks. Otherwise setup a simple form to submit to a PHP script that implements the listSubscribe() API call

The sample code is shown below:

/**
This Example shows how to Subscribe a New Member to a List using the MCAPI.php 
class and do some basic error checking.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

$merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account', 
                  'GROUPINGS'=>array(
                        array('name'=>'Your Interests:', 'groups'=>'Bananas,Apples'),
                        array('id'=>22, 'groups'=>'Trains'),
                        )
                    );

// By default this sends a confirmation email - you will not see new members
// until the link contained in it is clicked!
$retval = $api->listSubscribe( $listId, $my_email, $merge_vars );

if ($api->errorCode){
	echo "Unable to load listSubscribe()!\n";
	echo "\tCode=".$api->errorCode."\n";
	echo "\tMsg=".$api->errorMessage."\n";
} else {
    echo "Subscribed - look for the confirmation email!\n";
}

Once a member has been subscribed, if you want to add this in your website database.
you can do so and store the member mailchimp id for synchronisation by calling a method called listMemberInfo() . The sample code is shown below:

$retval = $api->listMemberInfo( $listId,$my_email);
	if ($api->errorCode){
	$msg =  "Mailchimp:Unable to load listMemberInfo()!\n";
echo "\tCode=".$api->errorCode."\n";
	echo "\tMsg=".$api->errorMessage."\n";
            }	else{
$mailchimp_id = $retval['data'][0]['id']; }

CREATE CAMPAIGNS TO SEND NEWSLETTERS

Each newsletter that you want to send is done by creating a campaign. The sample code to create a campaign is below:

Create a campaign for a future date

/**
This Example shows how to schedule a campaign for future delivery
via the MCAPI class.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

$schedule_for = '2018-04-01 09:05:21';
$retval = $api->campaignSchedule($campaignId, $schedule_for);

if ($api->errorCode){
	echo "Unable to Schedule Campaign!";
	echo "\n\tCode=".$api->errorCode;
	echo "\n\tMsg=".$api->errorMessage."\n";
} else {
	echo "Campaign Scheduled to be delivered $schedule_for!\n";
}

Create a campaign to send immediately

/**
This Example shows how to immediately send a Campaign via the MCAPI class.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

$retval = $api->campaignSendNow($campaignId);

if ($api->errorCode){
	echo "Unable to Send Campaign!";
	echo "\n\tCode=".$api->errorCode;
	echo "\n\tMsg=".$api->errorMessage."\n";
} else {
	echo "Campaign Sent!\n";
}

UNSUBSCRIBE A MEMBER

To unsubscribe a member from a campaign, use the sample code below:

/**
This Example shows how to pull the Info for a Member of a List using the MCAPI.php 
class and do some basic error checking.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

$retval = $api->listUnsubscribe( $listId,$my_email);
if ($api->errorCode){
    echo "Unable to load listUnsubscribe()!\n";
	echo "\tCode=".$api->errorCode."\n";
	echo "\tMsg=".$api->errorMessage."\n";
} else {
    echo "Returned: ".$retval."\n";
}

DELETE A CAMPAIGN

Lastly if you want to delete a campaign the code is shown below:

/**
This Example shows how to send Delete Campaigns via the MCAPI class.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

$retval = $api->campaignDelete($campaignId);

if ($api->errorCode){
	echo "Unable to Delete Campaign!";
	echo "\n\tCode=".$api->errorCode;
	echo "\n\tMsg=".$api->errorMessage."\n";
} else {
	echo "Campaign Deleted!\n";
}

Categories: Apache/PHP

amit

3 Responses to “Integrate the Mailchimp API with your PHP application”


Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe
legal online pharmacies mail order online pharmacy