New php library for the CM api

Hi all,

I wasn't impressed with the size and complexity of the php library for CM, so I wrote my own.
Its not as robust as the official class, but it does everything I have need to do so far.
It'd be great to get feedback from the community.
Also its php5 only.

Cheers,
Tobz.


<?php
/**
 * Campaign Monitor API Class
 *
 *
 * Toby Evans 2009
 * tobz.nz@gmail.com
 *
 * usage: 
 * $cm = new Campaignmonitor($api_key);
 * $response = $cm->call('Client.GetDetail', array('ClientID'=>'xxxxxx', ));
 */

class Campaignmonitor {

  private $api_url = 'http://api.createsend.com/api/api.asmx/';
  private $api_key = '';
  private $call_url = '';
  private $error = false;
  private $error_message = '';

  function Campaignmonitor($api_key = false) {
    // set API Key if present
    if (is_array($api_key)) {
      $this->api_key = $api_key[0];
    }
    else {
      $this->api_key = $api_key;
    }
  }

  /**
   * Set/Get API Key
   *
   * @param String $key [optional]
   * @return String
   */
  function api_key($key = false) {
    if ($key) $this->api_key = $key;
    return $this->api_key;
  }

  /**
   * Call an api Method via http
   * Returns false on error
   *
   * @param String $method
   * @param Array $input [optional]
   * @return Array
   */
  function call($method, $input = array(), $format='array') {
    $params = '?ApiKey='.$this->api_key;
    if (sizeof($input)) $params .= '&'.http_build_query($input, false, '&');
        $this->call_url = $this->api_url.$method.$params;
    $response = @simplexml_load_file($this->call_url) or $this->set_error('API Error: '.urldecode($this->call_url));

        if (isset($response->Code)) {
            $this->set_error($response);
            return false;
        }
        else if (!$response) {
            $this->set_error('API Error: '.urldecode($this->call_url));
            return false;
        }
        else {
            return $response;
        }
  }

    /**
     * Set an error message
     *
     * @param Int $code
     * @param String $message
     * @return String
     */
    function set_error($error) {
        if (is_string($error) || !$error) {
            $this->error_message = $error;
        }
        else {
            $this->error_message .= $error->Message;
            $error->call_url = $this->call_url;
            return $this->error = $error;
        }
    }

    function get_error() {
        return $this->error;
    }

    /**
     * Show the error message
     *
     * @return String
     */
    function show_error() {
        return $this->error_message;
    }

}
JCook, 7 years ago

It looks good! Funnily enough I had come to the same conclusion myself and have been writing my own PHP 5 class this week. It uses PHP's native soapclient class to handle the calls and responses from Campaign Monitor. If you're interested let me know and I'll post the code here as well (although it's still very much in development).

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free
1-888-533-8098