I am wondering if there is any simple example how to add new contact to a list using php. I found in your documentation sample code for php, but this is not a very simple example, the package has 1000+ files and is more than 5Mb. I can not include this into our extension that are sold to our customers. Is that necessary for just simple example how to add new contact, is there any simplier example?
Tnx!
I managed to create sample code myself. This is code for checking and adding new contact. 47 lines of code, much simplier than 1000+ files and 30000+ lines of code:)
<?php
$apiKey = "YOUR_API_KEY";
$token = "YOUR_TOKEN";
$listId = "YOUR_LIST_ID";
$email = "test@test.si";
$url = "https://api.constantcontact.com/v2/contacts?email=$email&status=ALL&limit=50&api_key=$apiKey";
$header[] = "Authorization: Bearer $token";
$header[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch));
curl_close($ch);
if(curl_error($ch)) echo 'error:' . curl_error($ch);
if(!$response->results){
$url = "https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=$apiKey";
$body = '{
"lists": [
{
"id": "'.$listId.'"
}
],
"confirmed": false,
"email_addresses": [
{
"email_address": "'.$email.'"
}
],
"first_name": "Ronald",
"last_name": "Martone"
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if(curl_error($ch)) echo 'error:' . curl_error($ch);
var_dump($response);
}
I like your example. You helped me out alot.
Very nice. Seems to be the only working example in the whole internet. Good job.
Worked like a charm! Thanks!!
Thanks for the start. I enhanced it a bit, to allow adding existing user to a list. We manage multiple lists.
<?php
//error_reporting(E_ALL);
$apikey = "API KEY";
$token = "AUTHENTICATION TOKEN";
$listid = "LIST ID";
$email = "EMAIL";
$first_name = "FIRST NAME";
$last_name = "LAST NAME";
$url = "https://api.constantcontact.com/v2/contacts?email=$email&status=ALL&limit=50&api_key=$apikey";
$header[] = "Authorization: Bearer $token";
$header[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch));
curl_close($ch);
if(curl_error($ch))
{
echo 'CURL Error:' . curl_error($ch);
exit;
}
// Check if exists and on list already
$list_array = [$listid]; // new list
$contactid = '';
if($response->results)
{
$contactid = $response->results[0]->id;
$lists = $response->results[0]->lists;
// Need to save existing lists!
foreach($lists as $obj)
{
if ($obj->id == $listid) continue; // already on this list
$list_array[] = $obj->id;
}
}
// If contact exists, will update, else add.
// If updating, can even change name, other fields. Probably just add a new list, tho.
if ($contactid)
{
$contactid = "/$contactid";
}
$url = "https://api.constantcontact.com/v2/contacts$contactid?action_by=ACTION_BY_VISITOR&api_key=$apikey";
$body = '{
"lists":
[';
foreach ($list_array as $listid)
{
$body .= '{"id": "' . $listid . '"},';
}
$body = substr($body,0,strlen($body)-1); // drop last comma
$body .= '],
"confirmed": false,
"email_addresses":
[
{"email_address": "'. $email .'"}
],
"first_name": "' . $first_name . '",
"last_name": "' . $last_name . '"
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
if ($contactid)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // PUT to update contact
}
else
{
curl_setopt($ch, CURLOPT_POST, 1); // Post to add contact
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
curl_close($ch);
if(curl_error($ch))
{
echo 'CURL Error:' . curl_error($ch);
exit;
}
echo "Response: ";
var_dump($response);
?>
Small fix to prior...check for CURL errors before closing...
<?php
//error_reporting(E_ALL);
$apikey = "API KEY";
$token = "AUTHENTICATION TOKEN";
$listid = "LIST ID";
$email = "EMAIL";
$first_name = "FIRST NAME";
$last_name = "LAST NAME";
$url = "https://api.constantcontact.com/v2/contacts?email=$email&status=ALL&limit=50&api_key=$apikey";
$header[] = "Authorization: Bearer $token";
$header[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch));
if(curl_error($ch))
{
echo 'CURL Error:' . curl_error($ch);
exit;
}
curl_close($ch);
// Check if exists and on list already
$list_array = [$listid]; // new list
$contactid = '';
if($response->results)
{
$contactid = $response->results[0]->id;
$lists = $response->results[0]->lists;
// Need to save existing lists!
foreach($lists as $obj)
{
if ($obj->id == $listid) continue; // already on this list
$list_array[] = $obj->id;
}
}
// If contact exists, will update, else add.
// If updating, can even change name, other fields. Probably just add a new list, tho.
if ($contactid)
{
$contactid = "/$contactid";
}
$url = "https://api.constantcontact.com/v2/contacts$contactid?action_by=ACTION_BY_VISITOR&api_key=$apikey";
$body = '{
"lists":
[';
foreach ($list_array as $listid)
{
$body .= '{"id": "' . $listid . '"},';
}
$body = substr($body,0,strlen($body)-1); // drop last comma
$body .= '],
"confirmed": false,
"email_addresses":
[
{"email_address": "'. $email .'"}
],
"first_name": "' . $first_name . '",
"last_name": "' . $last_name . '"
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
if ($contactid)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // PUT to update contact
}
else
{
curl_setopt($ch, CURLOPT_POST, 1); // Post to add contact
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
if(curl_error($ch))
{
echo 'CURL Error:' . curl_error($ch);
exit;
}
curl_close($ch);
echo "Response: ";
var_dump($response);
?>
Announcements
Join our list to be notified of new features and updates to our V3 API.
Sign Up