using PHP wrapper to add email address

This should be pretty straightforward, but I think the real problem is that I'm having trouble finding up-to-date and detailed documentation about the API.  There was another article in this forum that should have answered my question (id=4392), but when I wrote my code the way it was done in that example it didn't work; that developer was creating a new CampaignMonitor, and I don't even see that class listed in the wrapper code that I downloaded yesterday (12/6/2010).

So, here's the PHP code that I'm using:

    $email = $_GET["email"];
    $list_id = '<my list ID>';
    $api_key = '<my API key';
    $protocol = 'https';
    $debug_level = CS_REST_LOG_VERBOSE;
    $host = '';
    $log = NULL;
    $serialiser = NULL;
    $transport = NULL;
    $subscriber->EmailAddress = $email;
    $subscriber->Name = "";
    $subscriber->CustomFields = NULL;

    $cm = new CS_REST_Subscribers($list_id, $api_key, $protocol, $debug_level, $host, $log,     $serialiser, $transport);
    $result = $cm->add($subscriber);
    if ($result->was_successful()) {
        echo "Success!";
    } else {
          echo "oops!";

Could someone please let me know what I'm doing wrong, and perhaps more importantly, point me to where in the documentation I should have found out how to do this on my own?  Thanks so much!

phripley phripley, 7 years ago

I'm new to this too.  Did you look at the Subscriber.Add.php example?

Looks like this:

    //Sample using the CMBase.php wrapper to call Subscriber.AddWithCustomFields from any version of PHP
    //Relative path to CMBase.php. This example assumes the file is in the same folder
    //Your API Key. Go to to see where to find this and other required keys
    $api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    $client_id = null;
    $campaign_id = null;
    $list_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $list_id );
    //Optional statement to include debugging information in the result
    //$cm->debug_level = 1;
    //This is the actual call to the method, passing email address, name.
    $result = $cm->subscriberAdd('', 'Joe Smith');

    if($result['Result']['Code'] == 0)
        echo 'Success';
        echo 'Error : ' . $result['Result']['Message'];
    //Print out the debugging info

For my purpose it turned out that what I really wanted was subscriberAddAndResubscribe() not subscriberAdd() tho...

phripley phripley, 7 years ago

Achh. Seems that you are asking about API v3 while I am talking about API v2.  Will let someone with experience follow up with you here.

tobyb tobyb, 7 years ago


A good place to start for usage of the PHP wrapper is the samples directory. We've included sample code for all API resources. Take a look at samples/subscriber/add.php

It looks like your providing an object to the wrapper rather than an array as expected.

present.occupant, 7 years ago

Success!  Thanks, tobyb...hadn't noticed the sample code.  And thanks to you too, phripley, for the support!  Here's the code that I used that worked.

    $email = $_GET["email"];
    $list_id = '<my list ID>';
    $api_key = '<my API key>';
    $cm = new CS_REST_Subscribers($list_id, $api_key);
    $result = $cm->add(array(
    'EmailAddress' => $email,
    'Resubscribe' => TRUE
    if ($result->was_successful()) {
        echo "Subscribed with code ".$result->http_status_code;
    } else {
        echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
            echo '</pre>';   

See why 200,000 companies worldwide love Campaign Monitor.

From Australia to Zimbabwe, and everywhere in between, companies count on Campaign Monitor for email campaigns that boost the bottom line.

Get started for free