Join our Ask A Trainer Sessions and Chat With our Experts!

inserting new contact via php

JasonR642
Campaign Contributor
0 Votes

I am looking for the most basic way to insert a contact into CC using php. I know am having an issue trying to get the oauth2 code I found online to work (not sure how to get the tocken and then hard code it into my code)

 

So I am trying to use the below php but my issue is no new contact is actually being inserted so does CC not allow something as basic as the below code??

 

[code]


$emailaddress = "s@yahoo.com"; //yes hard coding this for testing purposes.

$UN = "removed";
$PW = "removed";
$Key = "removed";
$entry = '<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text"> </title>
<updated>2012-12-18T19:29:06.096Z</updated>
<author></author>
<id>data:,none</id>
<summary type="text">Contact</summary>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/ ">
<EmailAddress>' . $emailaddress . '</EmailAddress>
<OptInSource>ACTION_BY_CONTACT</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/removed/lists/13 " />
</ContactLists>
</Contact>
</content>';
// Initialize the cURL session
$request ="https://api.constantcontact.com/ws/customers/removed/contacts";
$session = curl_init($request);
// Set up digest authentication
$userNamePassword = $Key . '%' . $UN . ':' . $PW ;
// Set cURL options
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS , $entry);
curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));
curl_setopt($session, CURLOPT_HEADER, false); // Do not return headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); // If you set this to 0, it will take you to a page with the http response
// Execute cURL session and close it
$response = curl_exec($session);
curl_close($session);

[/code]

 

All I want to do is via Curl insert basic contact info into constant contact I rather not have to use a big library with a dozen files in it. Suggestions
Or can someone point me to a tutorial on hopw to generate a 0auth2 token so I can then hard code it into the php library that references a CTCTDataStore()

 

Thanks

9 REPLIES 9
Shannon_W
Employee

Hi,

 

You should be able to make a simple request to our servers like you showed.  What error are you receiving when you make the call?  Regardless, you don't have to generate an access token to use the PHP library, and I do recommend using the library since it handles curl and authentication.  You can use the wrapper with basic authentication, requiring only your username, password, and API Key, which you can hard code into config.php (keeping other lines for access token, etc, commented out).

 

Here is my sample code for adding or updating a contact using the PHP wrapper.

 

<?php

session_start ();
require_once 'ConstantContact.php';
require_once 'config.php';

//$ConstantContact = new ConstantContact("oauth2", $apiKey, $username, $accessToken);  //OR
ConstantContact = new ConstantContact("basic", $apiKey, $username, $password);
//Supply your credentials--API Key and other details--in config.php

// Get potential contact lists
$lists = $ConstantContact->getLists();
// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50

// Email address here is used for testing purposes
$emailAddress = "test_12132012@example.com";

// Search for our new Email address
$search = $ConstantContact->searchContactsByEmail($emailAddress);

//Debugging
echo '<pre>';
print_r($ConstantContact);
echo '</pre>';
echo $emailAddress . "<br>";

// If the search didnt return a contact object
if($search == false)
{
	// Create a new Contact Object to store data into
	$contactObj = new Contact();
	// Adding multiple lists to this new Contact Object
	$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
	// Set the email address
	$contactObj->emailAddress = $emailAddress;
	//Set the opt in source
	$contactObj->optInSource = "ACTION_BY_CONTACT";
	// Create the Contact and DONE
	$Contact = $ConstantContact->addContact($contactObj);
	echo ("Contact added.");

} // Otherwise we update our existing contact
else 
{
	// Gather data from our previous search and store it into a data type
	$contactObj = $ConstantContact->getContactDetails($search[0]);

	// We need to get the old list and add a new list to it as
	// this request requires a PUT and will remove the lists
	// as they are stored in an array
	 array_push($contactObj->lists, $lists['lists'][1]->id );
	 //Set the opt in value
	 $contactObj->optInSource = "ACTION_BY_CONTACT";
	// Update the contact and DONE
	$UpdateContact = $ConstantContact->updateContact($contactObj);
	echo ("Contact updated.");
}	

?>

 

Best Regards,

Shannon W.

API Support Specialist

JasonR642
Campaign Contributor
0 Votes

thank you for the reply

 

So using the code listing in your post I uploaded it in a php file in the same folder I have my config and constantcontact.php file and try to go to the url but I get an error message saying

failed to open stream: Permission denied in Components.php

 

ya I had losted my internet for a min and just realized 😞

Shannon_W
Employee
0 Votes

Okay, it sounds like you might have a connection issue.  Can you open a command line terminal and run a traceroute to us (api.constantcontact.com)?  

 

Best Regards,

Shannon W.

API Support Specialist

JasonR642
Campaign Contributor
0 Votes

OK I am getting a  Permission denied in Components.php error message on line 1446

Shannon_W
Employee
0 Votes

Hi,

 

Have you set your credentials--API Key, username & password for basic authentication, or API Key, username & access token for OAuth2 in config.php?  I would check to make sure they are input correctly, with no spaces, etc.

 

Best Regards,

Shannon W.

API Support Specialist

JasonR642
Campaign Contributor
0 Votes

Hello

 

I am now getting a 

Error 400: The request contains errors in the common Atom sections, which lie outside or , such as , , or .

 

error message and not sure how to fix it?

JasonR642
Campaign Contributor
0 Votes

actually I figured out the issue once is the date formate I had in the xml was not good and I also needed to wrape my contactlist id row inside a  <ContactLists> </ ContactLists>

 

thanks for the help all 🙂

BrendaL030
Rookie
0 Votes

Your code is EXACTLY what I was looking for and really should be included in the CTCT-OAuth2 wrapper files -- espcially the line that specifies how to call basic authentication. The documentation in the config.php file is just plain confusing -- so thank you! Still, I have a concern about the last part of your code where you appear to repair something about the list that you searched. If I don't need to update existing emails -- and only add new ones do I need this code? For my purposes, if the email already exists in the contact list, I don't need to do anything.

 

// Otherwise we update our existing contact
else 
{
	// Gather data from our previous search and store it into a data type
	$contactObj = $ConstantContact->getContactDetails($search[0]);

	// We need to get the old list and add a new list to it as
	// this request requires a PUT and will remove the lists
	// as they are stored in an array
	 array_push($contactObj->lists, $lists['lists'][1]->id );
	 //Set the opt in value
	 $contactObj->optInSource = "ACTION_BY_CONTACT";
	// Update the contact and DONE
	$UpdateContact = $ConstantContact->updateContact($contactObj);
	echo ("Contact updated.");
}	

DaveBerard
Employee
0 Votes

If you're only looking to add new Contacts and never update existing ones, you can just leave the else block out entirely.  This code is in here assuming that if a Contact subscribes to the list through whatever flow you have, you want to make sure that the contact is added to that list (in addition to any other lists they may be on already).  That's the only thing this code does and can easily be left out. 

 

A best practice recommendation is that if the Contact is interacting with you directly, you message them that they are already on the list though if you aren't planning on updating them and giving them a success message.  If the Contact isn't interacting directly with your app, this isn't really important.

Dave Berard
Senior Product Manager, Constant Contact
Resources
Developer Portal

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

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up