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.

Add Existing Contact to new list through php xml API

Highlighted

Add Existing Contact to new list through php xml API

I currently have a constant contact API set up to add new contacts by submitting an XML output through the CURL command. However, the below script does not add existing contacts to the contact list I would like to add them to. Is there a way to check the output of curl_exec to see if the email exist and then add the email to the targeted contact list?

 

<?php
$ccuser = '';
$ccpass = '';
$cckey = '';


if($affiliateType == 1 || $affiliateType == 3)
$contactListId = 15;
else
$contactListId = 20;


$contactListId = (!is_array($contactListId)) ? array($contactListId) : $contactListId;

$post = new SimpleXMLElement('<entry></entry>');
$post->addAttribute('xmlns', 'http://www.w3.org/2005/Atom');

$title = $post->addChild('title', "");
$title->addAttribute('type', 'text');

$post->addChild('updated', date('c'));
$post->addChild('author', "");
$post->addChild('id', 'data&colon;,none');

$summary = $post->addChild('summary', 'Contact');
$summary->addAttribute('type', 'text');

$content = $post->addChild('content');
$content->addAttribute('type', 'application/vnd.ctct+xml');

$contact = $content->addChild('Contact');
$contact->addAttribute('xmlns', 'http://ws.constantcontact.com/ns/1.0/');

$contact->addChild('EmailAddress', $email);
$contact->addChild('FirstName', $f_name);
$contact->addChild('LastName', $l_name);
$contact->addChild('OptInSource', 'ACTION_BY_CUSTOMER');

$contactlists = $contact->addChild('ContactLists');

// loop through each of the defined contact lists
foreach($contactListId AS $listId) {
$contactlist = $contactlists->addChild('ContactList');
$contactlist->addAttribute('id', 'http://api.constantcontact.com/ws/customers/' . $ccuser . '/lists/' . $listId);
}

$posturl = "https://api.constantcontact.com/ws/customers/{$ccuser}/contacts";
$authstr = $cckey . '%' . $ccuser . ':' . $ccpass;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $authstr);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post->asXML());
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/atom+xml"));
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$response = curl_exec($ch);

curl_close($ch);
?>

3 REPLIES 3
Highlighted
Employee

Re: Add Existing Contact to new list through php xml API

Hi,

 

To update an existing subscriber using the API, you can follow the instructions on this page.  You could do a search by email address call as on this page, then add or update the contact as appropriate once that's complete.  

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted

Re: Add Existing Contact to new list through php xml API

I am new to cURL and am not too certain how I recieve data back from the transfer to tell if the email exist or not. I took the same cURL process as above and came up with the below: 

 

$posturl = "https://api.constantcontact.com/ws/customers/{$ccuser}/contacts?email=webmaster%40solarsystemsusa.ne...";
$authstr = $cckey . '%' . $ccuser . ':' . $ccpass;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $authstr);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_HEADER, false); //Do not return headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$response = curl_exec($ch);

curl_close($ch);

 

 

I verified that it is finding the email address, but am having a hard time finding the variables to create a conditional statement for an existing contact. How can I capture a variable that signifies the email exists?

 

Tried a few other CURL commands from the php manual: http://php.net/manual/en/book.curl.php but with no success.

 

 

Highlighted
Employee

Re: Add Existing Contact to new list through php xml API

Hi,

 

Especially if you're new to curl, I would highly recommend not reinventing the wheel and using our PHP wrapper here.  Once you've downloaded the wrapper, you can use this example as a jumping off point to add existing users to your desired list(s)!  You may want to delete the form there and just use the PHP code as your starting point.

 

Best Regards,

Shannon W.

API Support Specialist

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