FuelPHP Package for Working with Campaign Monitor API

Continuing my “Do it Yourself” exploratory programming trend, I took one look at the CampaignMonitor SDK today and decided I’d write my own. Their API is RESTful, well documented, and easy to work with, so why not?

Here is the final product: https://github.com/calvinfroedge/FuelPHP—CampaignMonitor-API-Package

What I like about this API wrapper is the simplicity, and specifying the HTTP verbs to be used for the request. For instance, if you want to do a GET request on lists, you do:

CampaignMonitor::lists('GET');

Or say you want to add a subscriber to a list. Just do…

CampaignMonitor::subscribers(‘POST’, ‘list_identifier’, $subscriber_details);

Pretty easy, right? I designed the class so that you can pass the params flexibly. The only requirements are:

1. Put your verb first
2. Include any method params in the same order they occur in the API documentation. IE http://api.createsend.com/api/v3/lists/{listid}/stats.{xml|json} should be formatted as:

CampaignMonitor::lists('GET', 'list_identifier', 'stats');

…since the identifier occurs before ‘stats’ in the API endpoint.

Other than that, you can pass an array of params to be included in curl post fields, and another optional array to add a query string onto the endpoint. Again, you can include these in any order. Here’s the snippet that helps out here:

foreach($params as $k=>$v)
{
if(is_array($v))
{
if(isset($v['qs_params']))
{
$added = 0;
foreach($v['qs_params'] as $p=>$pv)
{
if($added == 0)
{
$query_string = '?';
}
$query_string .= "$p".'='."$pv".'&';
++$added;
}

//Remove the trailing &
self::$_query_string = substr($query_string, 0, -1);
}
else
{
self::$_params = json_encode($v);
}
}

if(!is_array($v))
{
self::$_method .= '/'.$v;
}
}

The response handling almost certainly isn’t perfect (I only tested a couple methods), but it worked for the handful of methods I tested with. My disclaimer is that working with strings is messy business, so before you put this into production use make sure you test it ; )

This entry was posted in FuelPHP, PHP, Programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Posted July 19, 2011 at 7:24 am | Permalink

    Hey Calvin,

    A nice API wrapper, like the idea of the way it’s setup.

    Just an idea, but something like (http://us3.php.net/manual/en/function.httpquerystring-set.php) might help with the query string building, I only found it myself recently.

  2. Posted July 19, 2011 at 2:55 pm | Permalink

    Nice work Calvin, I love the use of magic methods wherever they can be employed.

  3. Calvin Froedge
    Posted August 17, 2011 at 10:46 pm | Permalink

    Thanks for the feedback, guys!

  4. Chevalier Alexis
    Posted August 8, 2012 at 7:49 am | Permalink

    Hi !
    Thanks a lot for this package, it’s really a pleasure to work with :)

    However, i just got a problem with some responses from CampaignMonitor, if i get some dates with HH:MM:SS or URLs (http://..) (strings which contains a colon), the part of the string after the colon is empty (Ex : “2012-03-22 17″ instead of “2012-03-22 17:02:34″ or “http” instead of the complete URL).
    Have you an idea to fix this ?
    Thanks :)

  5. CHEVALIER Alexis
    Posted August 8, 2012 at 8:08 am | Permalink

    I fixed it,
    I just set a limit to the explode function in _parse_success and _parse_failure functions in the class campaignmonitor.php :
    $piece_ex = explode(‘:’, $p, 2); //for _parse_success function
    $piece_ex = explode(‘:’, $v, 2); //for _parse_failure function

    Have a nice day :)

Post a Comment

Your email is never published nor shared. 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>