Welcome Guest, Not a member yet? Register   Sign In
Library: Campaign Monitor API
#3

[eluser]Tobz[/eluser]
I ended up writing my own CM library. Here it is in case anyone wants to use it.

usage:

Code:
$this->load->library('campaignmonitor', array($api_key));
$respsonse = $this->campignmonitor->call('Client.GetDetail', array('ClientID'=>'xxxxxxxxxxx'));

The Class:
Code:
<?php
/**
* Campaign Monitor API Class
*
*
* Toby Evans 2009
* [email protected]
*
* 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;
    }

}


Messages In This Thread
Library: Campaign Monitor API - by El Forum - 05-21-2009, 07:40 PM
Library: Campaign Monitor API - by El Forum - 07-16-2009, 07:49 AM
Library: Campaign Monitor API - by El Forum - 11-26-2009, 04:13 AM



Theme © iAndrew 2016 - Forum software by © MyBB