The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

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

Highlighted
Member

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
Highlighted
Honored Contributor

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

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
Highlighted
Member

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

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

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured