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.

Listing more than 50 contacts and strange deletion issue

Highlighted
New Member

Listing more than 50 contacts and strange deletion issue


#1. I have couple of contact list with a thousand contacts. I'm able to get the list of first 50. How do I list get a listing of the remaining contacts?

Code is as follows:

function getContacts($listID) {
    $tcc = new ConstantContact(APIKEY);
    return $tcc->getContactsFromList(ACCESS_TOKEN, $listID);
}

$cc = new ConstantContact(APIKEY);

// Attempt to fetch lists in the account
try {
    $lists = $cc->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }
}

foreach ($lists as $list) {
    echo "<h2>" . $list->id . ": " . $list->name . "</h2>";

    // Attempt to get the emails from the list
    try {
        $contacts = getContacts($listID);
    } catch (CtctException $ex) {
        die();
    }

    foreach ($contacts->results as $contact) {
        echo $contact->id . ": " . $contact->company_name . ": " . $contact->email_addresses[0]->email_address . "<br>";
    }
}

 


#2. After I delete an email id, I'm no longer able to add that email id to any contact list.

I get the following error - "Array ( [error_key] => http.status.email_address.conflict [error_message] => Email address john@doe.com already exists. )". Although, the email is nowhere in the contact list.

I'm also not able to add the email using Constant Contact website.

Code is as follows:

 

foreach ($contacts->results as $contact) {
    try {
        $contactDel = new Contact();
        $contactDel->id = $contact->id;

        $returnContact = $cc->deleteContactFromList(ACCESS_TOKEN, $contactDel, $listKey);

    } catch (CtctException $ex) {
        foreach ($ex->getErrors() as $error) {
            print_r($error);
        }
    }
}

 

4 REPLIES 4
Highlighted
Honored Contributor

Re: Listing more than 50 contacts and strange deletion issue

Hello,

 

I can definitely assist you with both questions! For the first question, the getContacts function needs to be modified to request a larger number of contacts as once (up to 500), and also to support pagination from the API. The function will look a bit like this when it is done:

function getContacts($listID) {
    $tcc = new ConstantContact(APIKEY);
// Initialize a blank array to hold data across multiple pages.
    $contacts = Array();
// Map the parameters that we want to send to the API.
$params = Array("limit" => "500");
// Fetch the result from the API.
$result = $tcc->getContactsFromList(ACCESS_TOKEN, $listID, $params);
// Add that result to our results array. array_merge($contacts,$result->results);

// You will want to loop this next section until you have all of your results. For the simplicity of the example I have not added the loop.

// If there is a next page, continue.
if ($result->next != null)
{
// Set up the params with the pagination link.
    $params = Array("next" => $result->next);
// Fetch the next page.
    $result = $tcc->getContactsFromList(ACCESS_TOKEN, $listID, $params);
// Merge the results back into our contacts array.
    array_merge($contacts,$result->results);
}
// Return the assembled array of contacts.
return $contacts; }

 

One important thing to keep in mind with this change is that the return value from the getContacts function is now the direct list of contacts rather than an object from which you need to access the results member.

 

This means that your code:

foreach ($contacts->results as $contact) {

 

Can now be:

foreach ($contacts as $contact) {

 

 

Now, to address the second issue, the answer is actually much simpler. In Constant Contact we identify each contact based on their email address for creating contacts. Because of this, once a contact has been created with an email address, the system will not allow you to use a POST request to create another contact. The result of this attempt to make a POST request is seen as a 409 Conflict error.

 

The answer to this is actually quite simple, but the implementation can be a bit more complex. The solution is to run a check by email address to see if the contact already exists in the Constant Contact account before using a POST to create a new contact. If the contact does already exist, then you would use a PUT request to update all of their data to match the request.

 

The concept behind this looks like this:

$contact = $ctct->getContactByEmail(<access_token>,<email>);
if ($contact->id != null) 
{
    // Update the Contact    
    // If you desire, you can simply make edits to $contact and submit that as the info to be updated since it contains the contact's information.
    $ctct->updateContact(<access_token>,<contact>,<action_by_visitor>);
} else 
{
    // Add a new contact
    $ctct->addContact(<access_token>,<contact>,<action_by_visitor>);
}

 

In this case, the action_by_visitor is a true/false that does two things:

1. If it is true, then a contact who is unsubscribed will be opted back in when updateContact is used.

2. If it is true, the contact will be sent either a welcome email (added) or a a notification of updated preferences (updated).

 

If you have any questions or problems, please let me know and I will be happy to assist!

 

Sincerely,

Elijah G.

API Support Specialist

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: Listing more than 50 contacts and strange deletion issue

I am using the php API v 2

 

Passing an array to getContactsFromList as the $param results in an exception "The query parameter limit[limit] is not supported." Passing an integer from 50-100 works fine. Passing Array("next" => $result->next) also returns an exeption and just passing $result->next result in an exception "Pagination parameter 'limit' is invalid. Must be a positive integer; 'limit' can be no larger than 500."

 

How do I get get getContactsFromList to get the next set of results?

Highlighted
Occasional Participant

Re: Listing more than 50 contacts and strange deletion issue

the solution is:

 

$result= $cc->getContactsFromList(ACCESS_TOKEN, $listID, "?next=$nextLink");

Highlighted
Occasional Advisor

Re: Listing more than 50 contacts and strange deletion issue

Here's a sample code that paginates the results. Two methods shown, using ListService and the main class.

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