Beginner warning...Getting Segment details

I have downloaded the PHP wrapper and am able to get the information I am looking for to display on the screen using two of the different examples...using lists get_segments gets me all of the lists and their segment ids which is what I am after. Using the segment get call gets me the details of of the segment and more specifically, the total number of active subscribers.

Where I'm stuck is I'd like to combine these pieces so I have one file that grabs each segment ID from the lists get_segments call and then grabs each of the segments total active subscribers, just outputting them to the screen in a simple way.

I'm not sure why I'm having such a hard time connecting the pieces. It's my first attempt at using the API but it doesn't seem like I should be struggling like I am.

If anyone has a kick start of what the PHP should look like it would be GREATLY appreciated.

Thank you,


steveharney, 5 years ago


Stig Stig, 5 years ago

Hey Nick, sorry for the lack of response here!

I've put together an example for you here, which results in an HTML list of your segments and the number of subscribers in each segment. I haven't much PHP in a while, but I think the following should work OK:


require_once 'csrest_lists.php';
require_once 'csrest_segments.php';

$apikey = 'Your API Key - ';
$listid = 'Your List ID - ';

$segments_wrap = new CS_REST_Lists($listid, $apikey);
$segments_result = $segments_wrap->get_segments();

if($segments_result->was_successful()) {
    echo "<ul>\n";
    foreach ($segments_result->response as $segment) {
        $segment_details_wrap = new CS_REST_Segments($segment->SegmentID, $apikey);
        $segment_details_result = $segment_details_wrap->get();
        if($segment_details_result->was_successful()) {
            echo "<li>";
            echo $segment_details_result->response->Title;
            echo ": ";
            echo $segment_details_result->response->ActiveSubscribers;
            echo "</li>\n";
    echo "</ul>\n";

You'll want to insert the correct path to your wrapper files, and your own API key and list ID of course, and then update the code depending on how you'd like the information to be presented.


The Campaign Monitor Blog – HTML email smarts to go with your good looks.

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