cancel
Showing results for 
Search instead for 
Did you mean: 

Errors trying to remove contact from list

Occasional Participant

Errors trying to remove contact from list

I'm trying to remove a contact from a list using the PHP API. Since there doesn't appear to be any methods that do this for me in the documentation, I'm doing it manually by looping through all the lists and then updating the contact using this code:

 

         foreach ($contact->lists as $key => $value) {
            if ($value->id == $listID) { 
               echo "Unsetting $listID\n";
               unset($contact->lists[$key]);
            }
         }
         try {
            $ret = $cc->contactService->updateContact(ACCESS_TOKEN, $contact);
         } catch (CtctException $ex) {
            foreach ($ex->getErrors() as $error) {
               print_r($error);
            }
         }

When I run this using my own email address and a list I know I'm on I get the following exceptions:

 

stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/0: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/1: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/2: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/4: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/5: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/6: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/7: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/8: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.field.invalid
    [error_message] => #/lists/9: Property was found but is not permitted at this location.
)
stdClass Object
(
    [error_key] => json.type.invalid
    [error_message] => #/lists: Value is of a disallowed type. Allowed types are: Array, Null.
)

 

Does anyone know what's going on? Am I missing something somewhere? Is there a built in method for doing this, as this way of doing it seems rather hackish? Like a "delList" counterpart to "addList"?

8 REPLIES 8
Moderator

Re: Errors trying to remove contact from list

Hello,

 

Thanks for reaching out to Constant Contact's API Support.

 

There is no delList command. You would update the contact which is already built in to the library and just include only the lists you want to have the contact on. For example if the contact where on list A, B, and C; then you did and update and only included list A, and B then list C would be removed from the Contact. If you remove all lists from the contact when you update it then the contact is effectively deleted.

 

The documentation for this is located here (https://developer.constantcontact.com/docs/contacts-api/contacts-resource.html?method=PUT) pay special attention to the NOTE: in the description.


Regards,
Jimmy D.
Tier II API Support Engineer
Occasional Participant

Re: Errors trying to remove contact from list

 

There is no delList command. You would update the contact which is already built in to the library and just include only the lists you want to have the contact on.


Right, I know that. I even said as much in my post. You are telling me to do what the code I posted above is trying to do, but I'm getting a ton of exceptions back when I try to run the update after removing one list from the array of lists within the Contact object.

 

 

I'm using the PHP SDK from here: https://github.com/constantcontact/php-sdk

And based my update calls on this demo code: https://github.com/constantcontact/php-sdk/blob/master/examples/addOrUpdateContact.php along with information gained from searching this forum. I can't find any information on the "Property was found but is not permitted at this location." exception that is being thrown. 

 

Moderator

Re: Errors trying to remove contact from list

It appears that you are looking to parse through the $contact->lists array and delete any element that matches your $listID variable, correct?

 

Rather than using a foreach to parse through every element in the $contact->lists array, you can use the array_search() function. This returns the key for the element that matches your specified $listID which you can then use to unset() that element from the array. See this example:

 

if(($key = array_search($listID, $contact->lists)) !== false) {
    unset($contact->lists[$key]);
}

 


Regards,
Jimmy D.
Tier II API Support Engineer
Occasional Participant

Re: Errors trying to remove contact from list

I implemented that change, but now it doesn't find the list at all.

 

In the contact->lists there is

 

 

 [3]=>
object(Ctct\Components\Contacts\ContactList)#155 (6) {
["id"]=>
string(10) "1254185970"
["name"]=>
NULL
["status"]=>
string(6) "ACTIVE"
["contact_count"]=>
NULL
["created_date"]=>
NULL
["modified_date"]=>
NULL
}

But array_search("1254185970", $contact->lists) is returning false, when I'd expect 3. 

 

 

If I code it to unset($contact->lists[3]) and then try to do an update (that's the only thing I change), I get the "Property was found but is not permitted at this location." errors as listed in the OP. So, even if the array_search did work, I'd still get the errors. :(

 

Is there something else I have to do before I can change a Contact's lists? Do I need to use or include something else?

Seperate, but releated, do you know why the list objects don't populate with information beyond the id and the status, as seen above? 

 

Occasional Participant

Re: Errors trying to remove contact from list

I managed to get this working by looping through the lists and rebuilding the array, but excluding the list I didn't want them to be on:

 

         foreach ($contact->lists as $key => $value) {
            $newLists = array();
            if ($value->id != $listID) {
               $newLists[] = $value;
            }
         }
         $contact->lists = $newLists;

Hopefully this code will help someone else. 

Moderator

Re: Errors trying to remove contact from list

Great news; glad you got this worked out!


Regards,
Jimmy D.
Tier II API Support Engineer
Frequent Visitor

Re: Errors trying to remove contact from list

Just to be sure I understand this, the only way to REMOVE a contact is to query it, then PUT the same data back with the lists removed.  There is no REMOVE method that would operate in the same manner as DELETE does.   If so, adding a REMOVE method to the API would be nice!

Moderator

Re: Errors trying to remove contact from list

Hi @BrianA96,

 

On an individual basis there is no REMOVE option. Our bulk endpoints have a REMOVE option, but that works with a file. You can find that documentation here: 

http://developer.constantcontact.com/docs/bulk_activities_api/bulk-activities-removecontacts-multipa...


Regards,
Jimmy D.
Tier II API Support Engineer