Adding Subscribers to Campaign Monitor Lists using PHP5 and SOAP

by Vid Luther on December 15, 2008

in php, web

This is a quick how to, for people interested in adding subscribers to their Campaign Monitor lists. What you’ll need:

  1. PHP 5 with SOAP built in.
  2. A Valid Campaign Monitor Account
  3. A Campaign Monitor list

What’s assumed:

  1. You have a form already that’s collecting name and email address
  2. You know how to do basic input filtering

## ALL VALUES BELOW WERE GENERATED BY A RANDOM STRING GENERATOR CHANGE VALUES BELOW ##

// Campaign Monitor API Key
define('CM_APIKEY','wuc4vab7fik9v');

// Campaign Monitor List ID
define('CM_ANALYZEROPTIN_LIST_ID','lub2iv6keg9fost2e');

// Which client you're doing this for.
define('CM_CLIENT_ID','kov1hal9at7nau2w');

## ALL VALUES ABOVE WERE GENERATED BY A RANDOM STRING GENERATOR CHANGE VALUES ABOVE ##

$client = new SoapClient("http://api.createsend.com/api/api.asmx?wsdl");

$params->ApiKey = CM_APIKEY;
$params->ListID = CM_ANALYZEROPTIN_LIST_ID;
#$params->clientID = CM_CLIENT_ID;

$params->Email = "someaddress@example.com";
$params->Name = "Example Name";

$result = $client->AddSubscriber($params);

Stay tuned for the next part in this series, which explains how to handle the $result and handle any errors.

  • Chris,
    You beat me to it :). I was out watching a movie :). Stay tuned for a more "robust" system in the next few weeks.
  • Chris Stromquist
    Nevermind, I figured it out. I mistyped:

    Changed From:
    $params->ListId = 'key';

    To:
    $params->ListID = 'key';
  • Chris Stromquist
    Hi, I keep getting an "Invalid ListId" error (code 101). I checked and rechecked to make sure the api key/list id was correct and it seems to be, however I can't get this to work. Any advice? Also, would love to see how you handle the result. I can't seem to access that object properly either. Seems to be an stdClass Object called "Subscriber.AddResult".

    I'm trying to do this:
    $params->...etc, etc..
    etc...
    $res = $client->AddSubscriber($params);
    echo $res->Code;

    Thanks for the help,
    Chris
  • BitChain,
    You're correct. I wanted to share the first step on how to do the connection and add in an ideal error free world first.

    As for the AddSubscriber adding dupes, in my tests that wasn't the case, I kept adding myself with the same email address in my tests, and only showed me added once. It did return a Success though. It's either a bug on their end, or a "feature" :).
  • BitChain
    a number of points about this example:

    1) SoapClient throws exceptions, so try/catch them.
    2) AddSubscriber will add the same same email twice. It's better to use AddAndResubscribe in my view to catch those who forget they've already subscribed.
blog comments powered by Disqus

Previous post:

Next post: