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

[eluser]Ben Hirsch[/eluser]
Hi all. This is probably not the most elegant solution but it was a nice way to get CM working with CI quickly.

1) Grab the CM php sample from here: http://code.google.com/p/campaignmonitor...loads/list

2) Take the file CMBase.php from the download, rename it to CampaignMonitor.php and put it in your /system/application/libraries/ directory.

3) Find the following function (within the CampaignMonitor class):

Code:
function CampaignMonitor( $api = null, $client = null, $campaign = null, $list = null, $method = 'get' )
    {
        CMBase::CMBase( $api, $client, $campaign, $list, $method );
    }

and replace it with:

Code:
function CampaignMonitor( $api = null, $client = null, $campaign = null, $list = null, $method = 'get' )
    {
        $this->CI =& get_instance();
        CMBase::CMBase( $this->CI->cm_config['api'], $client, $campaign, $this->CI->cm_config['list'], $method );
    }

4) Then you can use it like this:

Code:
function _add_to_cm($email) {

        $this->cm_config = array(

                            'api'        => 'xxxxxxxxxxxx',
                            'list'        => 'xxxxxxxxxxxx'
                        );

        $this->load->library('CampaignMonitor');
        $result = $this->campaignmonitor->subscriberAdd($email, ''); // 2nd paramter could be used for name.
        
    }

... and you can use this function in your controller. Replace the api and list values with your own. (more info here: http://www.campaignmonitor.com/api/required/) Of course, you can use any CM API method you want. Just look at the documentation. But this will get you started with tunneling commands through the CM wrapper (CMBase.php) without having to make too many edits to the file.
#2

[eluser]Tim Jukes[/eluser]
Just wanted to say thanks for this - works a treat!
#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;
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB