How to Get started adding a new contact using PHP

Occasional Participant

How to Get started adding a new contact using PHP

I'm currently in the process of analysing Constant Contact from a developer's perspective with a view to my company using it in the future.  I've got experience of programming in a number of different languages (C#, VB.Net, Html, Css, Basic Javascript) but never used PHP.  For my purposes I can only uses PHP so I have a learning curve to climb :-)  Happy days.  I can't use any type of .Net as the servers hosting our solution are Linux / PHP / Apache based.


Initially all I want to be able to do is add a contact to one or more lists, the lists need to be hard coded so that the user only has to insert their e-mail address.  I have the following questions.


1.  Is it possible to add a contact to a list using javascript?  or will I run into cross-site scripting issues?

2.  If I need to use PHP should I be using the 'new' wrapper?  or does this still have some early teething problems that would cause a problem to a newbie?

3. Is the 'new' wrapper found here -

4. Is there a really simple example of how to use this wrapper without a form that I can use to test it works, I'm thinking of a seriously basic php file that has everything hard-coded into it so it adds just one contact. 


Thanks in advance for any help or guidance anyone can provide





1.  You will most likely run into cross-site scripting issues if you try to do some code in Javascript and some in PHP.  If you know how to handle it, I believe it is possible.  StackOverflow has some good threads on how to use PHP and Javascript together, but we don't have any examples specific to us.


2/3.  Yes, that is the 'new' PHP wrapper, and it is what you should be using.  Whenever we make updates, that library gets updated, so you shouldn't have too many (if any) issues regarding the wrapper itself.


4.  The wrapper does contain a simple example of adding a contact with PHP here.


I hope that helps!


Best Regards,

Shannon W.

API Support Specialist

Occasional Participant

Thanks Shannon, that's cleared up a number of areas, I have a couple of follow-up questions:-


1. I was actually looking at a solution initially using only html and javascript using ajax calls to the api without any php.  I did some extensive searching and can't find any other people doing this, I'm assuming there is a good reason why not (maybe I'm wrong).

2. Is the only additional file I need (as well as addContact.php) - ConstantContact.php?

4. What value do I set for consumerSecret in addContact.php?

5. On line 12 of addContact.php do I need to modify the value for 'username' in $DatastoreUser['username']?

6. In order to hard-code a list would I do the following:-

a) remove this

    foreach($ContactLists['lists'] as $list){
        echo '<input type="checkbox" name="lists[]" value="'.$list->id.'"> '.$list->name.'<br />';
b) Change $Contact->lists = $_POST['lists'] to $postFields["lists"] = array("");
Thanks again for your quick response, it really helps.



You're welcome.


1.  Javascript can't make cross-domain requests since it's client-side only, so developers who use it that way have to use PHP or some other language as an intermediary to make the actual request (from your site to Constant Contact).


2.  You actually need Authentication.php, Collections.php, Components.php, ConstantContact.php, and your file (in this case addContact.php).


3.  No 3, haha.


4.  You want to change that to the consumer secret associated with your API Key, which can be found by going to the API Key settings page, which you'll see after you enter your login credentials.


5.  No, you don't need to change the username value there.  The datastore variable stores that value and calls it from that line.


6.  Your logic is good and I believe that code will work, but don't quite have the time to test it right now.  Let me know if you have problems with making that change (after getting the code to work as-is).


Best Regards,

Shannon W.

API Support Specialist



Occasional Participant

Thanks again.  I've spent some time trying to get the suggested example working but with no luck.  First I tried uploading the files you mentioned and then modifying the parameters as necessary within addContact.php. 


I received the following warning:-

Add Contact Form Example

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/n/i/c/nichole2/html/webinar/ABTest2/addContact.php:2) in /home/content/n/i/c/nichole2/html/webinar/ABTest2/addContact.php on line 3
OAuth Exception: Username {Username_Removed} not found in datastore Click here to authorize


It seems that we need more files than you suggested because the link to authorise sends me to page example_verification.php?apiKey=API_KEY_REMOVED&secret=SECRET_REMOVED&


So, I uploaded the example_verification file, this file seems to reference config.php so I modified this (not sure if I got this part right, more on this later).  example_verification includes a link to index.php, so I uploaded that as well.


So, now when I click on 'Click Here to Authorize' I get the following error:-


The page isn't redirecting properly  

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.


I took a look into config.php and set $apikey and $consumersecret but there are two more parameters that I don't know how to set.  $verificationURL talks about 'this must match up', I have now gone back to my apikey and set a value for redirect URI and set verificationURL to the same value.  I still get the same results from firefox when trying to authenticate.  So..... I still can't get the examples to work.  Seems mighty complicated, hopefully there is something simple I am missing.  The new wrapper seems to be far more complicated than the older one, I'm unclear what advantages it holds for me.


Here's the part from config.php, I modified $verificationURL only, not sure what to do with $returnURL or whether this makes a difference. 


// Redirect URL which is set when creating your API key, this must match up
// or you will receive an error
$verificationURL = '';

// This URL will be used in the example_verification to bounce back to wherever
// you like.  This would be a way to send back to where the user started
$returnURL = '';


Any suggestions on what I'm missing would be great.



Sorry for the frustration.  Now it sounds like the only issue you're having is with the redirection at the end of the authentication process (so the good news is that's almost over).


The returnURL should be set to whatever URL you want [your users, if anyone else is going to be authenticating besides yourself] to be sent to after they grant access to the integration (to their Constant Contact account).  If it's just this account, or internal accounts, that will need to use your integration, your returnURL can be set to a simple html/php page that simply displays the access token that is generated after you grant access.  


For example, I have an 'answer.php' file that contains the following code:



$apiKey = "set_this";
$consumerSecret = "set_this";
$redirectURI = "http://localhost:8080/Oauth2/answer.php"; //set this too

$code = $_REQUEST['code'];
$token = $_REQUEST['access_token'];

echo "<br><br><a href='$apiKe...
redirect_uri=$redirectURI'>Click Here to finish the OAuth process and generate your access token!</a>";

if ($_REQUEST['code'] != null)
	echo "<br><br><br><p>Click the link below if you have already generated an access token and added it to your files.</p><br>";
	echo "<a href='$token'>Contact</a>";




I hope that helps.  I shared a (hopefully) well-instructed set of files here to help users generate an access token to be used for OAuth 2 if you want to follow my example.  I know OAuth 2 can be very confusing, especially the first time around, and do rest assured that we are hoping to make the process simpler in the future by just giving developers an access token so they don't have to go through this process.  I apologize that we don't currently have that in place.


Best Regards,

Shannon W.

API Support Specialist

Occasional Participant



I've been tinkering with the PHP wrapper for a few days now, and I'm more than a little confused.


I've gone through all of the authentication steps, and I have obtained my token, but I can't for the life of me figure out how to put it to good use.


I have a website with a user registration / login system in place. When a user registers, I simply want the user to be added to a specific Constant Contact list. In my case, I am only collecting their first name / last name / email.


Here is how the flow works right now: Client creates an account, and the code from addContact.php is called with the list hard-coded (instead of being selected via a form, as is the case in the example). When that code is called, I get 'Username XXXXX not found in datastore. Click here to authorize.' which, of course, requires my Constant Contact username / password. Obviously, a prospect on my website creating an account does not have my credentials and could never proceed past this point.


Am I missing something here? It appears that all of the samples provided only work to return the OAuth2 token. I've already done that. Now, how to move forward and actually use the token to add a contact?



I know it's confusing and I apologize for that.  Our PHP library is set up to take more than just the username and token, although in theory, the Constant Contact username and token are all you need when using OAuth2 (once you've generated the token).  I have been doing a deep dive into the PHP wrapper to see what can be done to make it more OAuth2 friendly, but it's a slow process.


Right now, the simplest thing to do would be to store your username, password, and API Key as well as the token and consumer secret, and then modify the code to bypass the verification process.


Best Regards,

Shannon W.

API Support Specialist

Occasional Participant

can you give me full copy of this php module. thanks in advance.



If you are looking to use PHP to connect with Constant Contact's API, I would strongly reccommend using our new V2 API and the accompanying SDK that we have created for it. You can find the entire SDK (Along with a sample of how to add contact information) here:


If you have not already created a (free)developer account and set up your API key, I would suggest going through the steps outlined in our Get Started guide here:


Best Regards,

Elijah G.
API Support Engineer
Developer Portal

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

Visit Page