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.

v2 PHP SDK update contact

JennyS13
Regular Participant

v2 PHP SDK update contact

I'm working with the PHP SDK and running into an issue with updating an existing contact's status.

 

I'm able to add a contact no problem.

 

 

			$ccContact = Ctct\Components\Contacts\Contact::create([
				'status'			=> 'ACTIVE',
				'first_name'		=> $user->getFirstname(),
				'last_name'			=> $user->getLastname(),
				'email_addresses'	=> [[ 'email_address' => $user->getEmail() ]],
				'addresses'			=> [],
				'notes'				=> [],
				'custom_fields'		=> [],
				'home_phone'		=> $user->getMetadata()->getPrimaryPhone(),
				'lists'				=> [ ['id' => Config::get('constantcontact.list_id')] ]
			]);

			$ccContact = $cc->addContact(Config::get('constantcontact.access_token'), $ccContact);

 But I'm not able to update their satus:

$contact = $cc->getContact(Config::get('constantcontact.access_token'), $ccContact->results[0]->id);
$contact->status = 'OPTOUT';
$result = $cc->updateContact(Config::get('constantcontact.access_token'), $contact);
dumping $contact gives me a status of "OPTOUT" like I expect, but $result is "ACTIVE". Obviously it is not saving in the API either. Not getting any errors, just not updating. Thanks for your help.
10 REPLIES 10
JennyS13
Regular Participant

I should add to that the contact "Updated At" time is actually updating as well.  Maybe these aren't right or I'm not using them right?

 

 

    /**
     * Status of the contact, must be one of "ACTIVE", "UNCONFIRMED", "OPTOUT", "REMOVED", "NON_SUBSCRIBER", "VISITOR"
     * @var string
     */
    public $status;

Hi Jenny,

 

The contact status field is a read-only field.  You want to be using the deleteContact method to unsubscribe a contact.

 

Best Regards,

Shannon W.

API Support Specialist

JennyS13
Regular Participant

Oh okay, trying that now.

 

If someone is opted out just toggled to optback in, do I re-create the contact?  Or fetch the contact and edit it?

 

Thanks for your help.

JennyS13
Regular Participant

I am able to delete just fine now, but the situation I described above I don't see a solution for.

 

If I try to "create" a contact, it errors out saying the contact exists.  But the status field is readonly.  I can't think of a way to bring that contact back to "active".  The only methods I see are:

 

addContact

deleteContact

updateContact

Hi,

 

To opt a contact back in, you would get the contact by ID, add them to lists, and use the updateContact method, specifying the optional parameter for updateContact to set ACTION_BY_VISITOR to true.  I hope that helps!

 

Best Regards,

Shannon W.

API Support Specialist

JennyS13
Regular Participant

I'm still having issues when opting the user back in.

 

When I try to update an existing contact:

 

$result = $cc->updateContact(Config::get('constantcontact.access_token'), $contact, true);

 

I'm getting the following error:

 

Ctct/src/Ctct/Util/RestClient.php on line 93 with message "[{"error_key":"http.status.conflict","error_message":"There was a conflict between the supplied data and the existing resource."}]"

 

Note I'm passing the param of ACTION_BY_VISITOR as true as suggested.

JennyS13
Regular Participant

I believe that I figured it out.

 

For what it is worth, something that made it harder to work with was the API throwing exceptions for non-errors.  It is probably just a personal prefence, but if a user is already part of a list and they are re-added through the API, it made it harder throwing an exception.  

 

It just ended up that I had to put in all sorts of checks (and extra API calls) before running add/edit/delete.

 

Thank you for the help though, it is appreciated.

Sorry for the confusion there.  Since our API is a REST API, it supports creating (POST) new contacts or updating (PUT) existing contacts.  In REST, those actions are considered very different and our API treats them as such.  The exception which was through should have been a 409 Conflict error with the error_message indicating that the Contact already exists.  If you didn't see that, please let us know what you did see so we can go in and clear that up. 

 

Also, we tried very hard to document that and make that really clear in our code examples as well.  Any suggetions on how we could make that more clear so that others don't run into the same problems you did?  Thanks in advance for taking the time to give us any feedback!

Dave Berard
Senior Product Manager, Constant Contact
MAKM4
Regular Participant

Hi,

Please help in "There was a conflict between the supplied data and the existing resource" error.

I am using constant contact php-api and I have the exact same problem, but it does not occur while creating a new contact while the same error occurs when I am updating a contact (when confirmed opt-in is on he has confirmed the subscription). Also I dont get any error whether it is adding a new contact or updating an existing contact while the Confirmed opt-in is not used. Dont know what to do, the above solution didn't work for me.

Please help me.

 

Thanks,

MAK

Thanks for the report.  This is an issue we recently discovered and a bug on our side.  We provided a little additional information in our May release notes and are working on a fix for this: http://developer.constantcontact.com/docs/release-notes/may-2014-rel-notes.html

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page