I get an error when trying to import a contact with the anniversary field using the API php-sdk-3.0.0-alpha addOrUpdateContact.php. If I remove the Anniversary field it works fine.
I get the following error:
Array ( [0] => stdClass Object ( [error_key] => json.field.invalid [error_message] => #/anniversary: Property was found but is not permitted at this location. ) )
Here is my code: access codes removed.
<!DOCTYPE HTML>
<html>
<?php
// require the autoloaders
require_once 'src/Ctct/autoload.php';
require_once 'vendor/autoload.php';
use Ctct\Components\Contacts\Contact;
use Ctct\ConstantContact;
use Ctct\Exceptions\CtctException;
// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "xxxxxxxx");
define("ACCESS_TOKEN", "xxxxxxx");
$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;
}
}
// check if the form was submitted
if (isset($_POST['email']) && strlen($_POST['email']) > 1) {
$action = "Getting Contact By Email Address";
try {
// check to see if a contact with the email address already exists in the account
$response = $cc->contactService->getContacts(ACCESS_TOKEN, array("email" => $_POST['email']));
// create a new contact if one does not exist
if (empty($response->results)) {
$action = "Creating Contact";
$contact = new Contact();
$contact->addEmail($_POST['email']);
$contact->addList($_POST['list']);
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];
$contact->anniversary = $_POST['Anniversary'];
//$contact->anniversary_month = $_POST['anniversary_month'];
//$contact->anniversary_day = $_POST['anniversary_day'];
/*
* The third parameter of addContact defaults to false, but if this were set to true it would tell Constant
* Contact that this action is being performed by the contact themselves, and gives the ability to
* opt contacts back in and trigger Welcome/Change-of-interest emails.
*
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
*/
$returnContact = $cc->contactService->addContact(ACCESS_TOKEN, $contact, true);
// update the existing contact if address already existed
} else {
$action = "Updating Contact";
$contact = $response->results[0];
if ($contact instanceof Contact) {
$contact->addList($_POST['list']);
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];
$contact->anniversary_year = $_POST['anniversary'];
//$contact->anniversary_month = $_POST['anniversary_month'];
//$contact->anniversary_day = $_POST['anniversary_day'];
/*
* The third parameter of updateContact defaults to false, but if this were set to true it would tell
* Constant Contact that this action is being performed by the contact themselves, and gives the ability to
* opt contacts back in and trigger Welcome/Change-of-interest emails.
*
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
*/
$returnContact = $cc->contactService->updateContact(ACCESS_TOKEN, $contact, true);
} else {
$e = new CtctException();
$e->setErrors(array("type", "Contact type not returned"));
throw $e;
}
}
// catch any exceptions thrown during the process and print the errors to screen
} catch (CtctException $ex) {
echo '<span class="label label-important">Error ' . $action . '</span>';
echo '<div class="container alert-error"><pre class="failure-pre">';
print_r($ex->getErrors());
echo '</pre></div>';
die();
}
}
?>
<div class="well">
<h3>Add or Update a Contact</h3>
<form class="form-horizontal" name="submitContact" id="submitContact" method="POST" action="addOrUpdateContact.php">
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="control-group">
<label class="control-label" for="first_name">First Name</label>
<div class="controls">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="last_name">Last Name</label>
<div class="controls">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="anniversary">Anniversary</label>
<div class="controls">
<input type="text" id="anniversary" name="anniversary" placeholder="Anniversary">
</div>
</div>
<!-- <div class="control-group">
<label class="control-label" for="anniversary_month">Anniversary Month</label>
<div class="controls">
<input type="text" id="anniversary_month" name="anniversary_month" placeholder="Anniversary Month">
</div>
</div>
<div class="control-group">
<label class="control-label" for="anniversary_day">Anniversary Day</label>
<div class="controls">
<input type="text" id="anniversary_day" name="anniversary_day" placeholder="Anniversary Day">
</div>
</div>
-->
<div class="control-group">
<label class="control-label" for="list">List</label>
<div class="controls">
<select name="list">
<?php
foreach ($lists as $list) {
echo '<option value="' . $list->id . '">' . $list->name . '</option>';
}
?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">
<div class="controls">
<input type="submit" value="Submit" class="btn btn-primary"/>
</div>
</div>
</form>
</div>
<!-- Success Message -->
<?php if (isset($returnContact)) {
echo '<div class="container alert-success"><pre class="success-pre">';
print_r($returnContact);
echo '</pre></div>';
} ?>
Hi @TheresaL82,
Thank you for reaching out to Constant Contact's API Support.
The anniversary and birthday fields were added to our system after our current version of the API was created. We are currently working toward a new version of the API that will have these features. Until then you will need to import these features via the website UI.
The holidays have come and gone. For many seasonal businesses, this means the rush of shoppers has decreased as well. Instead of turning off the lights and waiting for spring, make your email marketi...
See Article