Check if user exists with subscriberGetSingleSubscriber

I want to check if a user exists in the campaign monitor database and what the user's status is if he exists. But I can't make the function below work. It always return success no matter what email I provide. When debugging I can se that it finds the correct path to the xml file and that this file contains the correct information. i.e. if a user is not in the list or not doesn't exist I would not expect to get success as I would expect the result code to be something like "203: Subscriber not in List".

Can anybody help me with this issue?

function get_subscriber($listid = 0, $email = ''){
        require_once('campaign-monitor/CMBase.php');
        
        $api_key = 'my_api_key_here';
        $client_id = 'my_client_id_here'; 
        $campaign_id = null;
        $cm_list_id = 'mylist_id_here';

        $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $cm_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.
        $cm_result = $cm->subscriberGetSingleSubscriber($cm_list_id, $email);
    
        if($cm_result['Result']['Code'] == 0)
            echo 'Success';
        else
            echo 'Error : ' . $cm_result['Result']['Message'];
}
Ben Ben, 6 years ago

Hi,

If your still experiencing this problem it might be worth getting in touch with support. We can examine the data sent during your API requests and should be able to get to the bottom of the issue pretty quickly.

jamesd jamesd, 6 years ago

Hi there,

The code you have written will ultimately submit a GET request whose response when successful will look something like this (as per the API documentation):

<?xml version="1.0" encoding="utf-8"?>
<anyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xsi:type="Subscriber" xmlns="http://api.createsend.com/api/" >
  <EmailAddress>string</EmailAddress>
  <Name>string</Name>
  <Date>string</Date>
  <State>string</State>
  <CustomFields>
    <SubscriberCustomField>
      <Key>string</Key>
      <Value>string</Value>
    </SubscriberCustomField>
    <SubscriberCustomField>
      <Key>string</Key>
      <Value>string</Value>
    </SubscriberCustomField>
  </CustomFields>
</anyType>

Upon failure the response will look something like:

<?xml version="1.0" encoding="utf-8"?>
<anyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="Result" xmlns="http://api.createsend.com/api/">
  <Code>203</Code>
  <Message>Subscriber not in list</Message>
</anyType>

The PHP wrapper you are using translates these XML responses into PHP, so your code needs to check what is returned. I've modified your code below, so that it should work as expected.

function get_subscriber($listid = 0, $email = '') {
        require_once('campaign-monitor/CMBase.php');
        
        $api_key = 'my_api_key_here';
        $client_id = 'my_client_id_here'; 
        $campaign_id = null;
        $cm_list_id = 'mylist_id_here';
        $cm = new CampaignMonitor( $api_key, $client_id, $campaign_id, $cm_list_id );
        $cm_result = $cm->subscriberGetSingleSubscriber($cm_list_id, $email);

        if($cm_result['anyType']['Code'])
                echo 'Error ' . $cm_result['anyType']['Code'] . ': ' . $cm_result['anyType']['Message'];
        else
                echo 'Success: ' . $cm_result['anyType']['EmailAddress'];
}
sfdam, 6 years ago

Thanks, that worked :)

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