Search code examples
phpcampaign-monitor

Campaign Monitor php sdk - add Subscribe bug - no longer showing on list


I am working on a campaign monitor api which creates a custom list with custom fields.

When I try and add subscribers it used to work, now when I look at the list its not added them. Although its still returning a success code 201.

function addSubscriber($list_id, $emailAddress, $name, $title, $showName, $showDate, $showTime){
    //create subscriber
    $subscriber = array(
        'EmailAddress' => $emailAddress,
        'Name' => $name,
        'CustomFields' => array(
            array(
                'Key' => "Title",
                'Value' => $title
            ),
            array(
                'Key' => "ShowName",
                'Value' => $showName
            ),
            array(
                'Key' => "ShowDate",
                'Value' => $showDate
            ),
            array(
                'Key' => "ShowTime",
                'Value' => $showTime
            )
        ),
        'Resubscribe' => true,
        'RestartSubscriptionBasedAutoResponders' => true
    );

    //print_r($subscriber);

    $subwrap = new CS_REST_Subscribers($list_id, $this->auth);
    $result = $subwrap->add($subscriber); 
    //var_dump($result->response);

    echo "Result of POST /api/v3.1/subscribers/{list id}.{format}\n<br />";
    if($result->was_successful()) {
        echo "Subscribed with code ".$result->http_status_code;
    } else {
        echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
        var_dump($result->response);
        echo '</pre>';
    }
    return $result->response;
}

Solution

  • This code is working for me.You need to add campaign monitor class files in you project folder and use valid list id and api key.You can find you api key from manage account link and list id from click on (change name/type) below list title. Please wait some time to see in you list.

    require_once 'csrest_subscribers.php';
    
    
    $name=$_POST['name'];
    $email=$_POST['email'];
    
    
    $wrap = new CS_REST_Subscribers('Your list ID', 'Your API Key');
    $result = $wrap->add(array(
        'EmailAddress' => $email,
        'Name' => $name,
        'CustomFields' => array(), // no custom fields, can remove this line completely
        'Resubscribe' => true
    ));
    
    
    echo "Result of POST /api/v3/subscribers/{list id}.{format}\n<br />";
    if($result->was_successful()) {
        echo "Subscribed with code ".$result->http_status_code;
    } else {
        echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
        var_dump($result->response);
        echo '</pre>';
    }