We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

PHP - Display number of existing contacts in an email list.

JodyS58
Occasional Participant

PHP - Display number of existing contacts in an email list.

How would I display the number of the contacts in an email ist on a PHP page?

 

I would like to display how many existing people have signed up for a certain contact list I created.

 

2 REPLIES 2
Elijah_G
Member

Hello,

 

If you're using our PHP SDK, The code would look like this:

<?php
// require the autoloaders
require_once '../src/Ctct/autoload.php';
require_once '../vendor/autoload.php';
use Ctct\Components\Contacts\Contact;
use Ctct\Exceptions\CtctException;

// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "ENTER YOUR API KEY");
define("ACCESS_TOKEN", "ENTER YOUR ACCESS TOKEN");

$cc = new ConstantContact(APIKEY);

// attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
try {
    $lists = $cc->listService->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }
    if (!isset($lists)) {
        $lists = null;
    }
}

$contactCount = 0;
// $lists contains all of your lists, you can search for a specific name
foreach ($lists as $list)
{
    if ($list->name == "LIST NAME")
    {
        $contactCount = $list->contact_count;
        break;
    }

}

echo $contactCount;
?>

 

If you'd prefer not to use the SDK, you could use CURL to perform the API call documented here:

http://developer.constantcontact.com/docs/contact-list-api/contactlist-collection.html

 

Let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
JodyS58
Occasional Participant

Great! Thanks that's exactly what I was looking for! 

Developer Portal

View API documentation, code samples, get your API key.

Visit Page