SAMPLE: (PHP) Create a new contact from your member registration form

Regular Participant

SAMPLE: (PHP) Create a new contact from your member registration form

Here's a sample script to create a new contact. It's assuming you have a member signup form on your page that has collected first name, last name, email address, and zip code.

This is based on the "SAMPLE: PUT (Update) a Contact (PHP)" script found here:
http://developer.constantcontact.com/samples/putSubscriber
------------------------------------

<?php

$first_name = trim($_POST);
$last_name = trim($_POST);
$email = trim($_POST);
$zipcode = $_POST;

/////////// REGISTER EMAIL WITH CONSTANT CONTACT ///////////////////

$UN = "your_member_name_here";
$PW = "your_password_here";
$Key = "your_API_key_here";

$entry = '<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text"> </title>
<updated>' . date('c') . '</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>' . $email . '</EmailAddress>
<FirstName>' . $first_name . '</FirstName>
<LastName>' . $last_name . '</LastName>
<PostalCode>' . $zipcode . '</PostalCode>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/' . $UN . '/lists/1" />' // Do this for all the lists you want to add to
. '<ContactList id="http://api.constantcontact.com/ws/customers/' . $UN . '/lists/2" />' // Be sure to get the correct list number(s) for your list(s)
. '</ContactLists>
</Contact>
</content>
</entry>';

// Initialize the cURL session
$request ="http://api.constantcontact.com/ws/customers/" . $UN . "/contacts";
$session = curl_init($request);

// Set up digest authentication
$userNamePassword = $Key . '%' . $UN . ':' . $PW ;


// Set cURL options
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
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);

////////////////// END REGISTER EMAIL WITH CONSTANT CONTACT ///////////////////////

// Your code goes here: typically sending a confirmation email, and/or a Header("Location: your_page.php") redirect

?>
11 REPLIES 11
Regular Participant

We need to be able to add date of birth to the form, and then have emails sent based on that data (Happy Birthday type stuff). Is there any way we can add this to this form? So for instance, my contact form would have a drop down for month and dropdown for day, and those variables would be passed to this form.

Secondly, can these date variables be sent to my CC list as just text and still be searchable within the CC database or do I have to specify in the API that they are integers?

Thanks so much, and in case my questions don't give it away, I don't quite know what I am doing, so I apologize if my questions are kind of dumb. Thanks for your help (and patience) in advance!

Peter
Moderator

Hi Peter,

What you're looking to do, sending out emails based on birthday, is currently a manual process. Constant Contact does not have a date based autoresponder feature. There also is no date sortable field in Constant Contact to do this process. The easiest way to do this is probably to use the Custom Fields to store the birthday and then export the names to excel on a regular basis. You can then pull out the birthdays by sorting based on that column. It's a little more manual than what you were looking for, but you will be able to do what you are looking to.
Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

I guess my question is could I use drop downs in the form as the custom fields? Or is everything text areas on the forms.

Also, using an API contact form could I do this?

Thanks!

Peter
Moderator

Using our API allows you to create your own form. This allows you to completely control how and what data the user is allowed to send to us. If you design your form to allow the user to select specific values from a drop down menu and want to send those values to us as a Custom Field, that is definitely something you can do.
Dave Berard
Senior Product Manager, Constant Contact
Frequent Participant

Please see also this PHP Sample.

Thanks,
Tom M
Group Product Manager – Content Editing
Constant Contact
Occasional Participant

Do you have the sample of the form code that goes along with this .php?
Occasional Participant

I'm presently using this code for a client, and wanted to check to see if any changes are needed to support the new API requirements recently sent out by Constant Contact (Basic Authentication over HTTPS).  I'm not entirely sure if I'm understanding the changes, and if the code above uses the REST API that the e-mail referred to.  Any help is appreciated.

Moderator

This code sample does use the REST API.  You will need to implement the changes described here.  

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

Okay, so just to make sure, the only change is:


curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);


to


curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);


As well as making the PHP script be called over https, instead of http, correct?

Moderator

Provided your Apache (or whichever platform you're running) already has SSL Modules installed and configured, this is all you need to do.  If you do not, that is actually something you will want to go to your host or vendor for support with as it is beyond the scope of the Constant Contact API.

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

For anyone else needing to correct their code to operate over BASIC authentication, you need to also change this line:


$request ="http://api.constantcontact.com/ws/customers/" . $UN . "/contacts";


to


$request ="https://api.constantcontact.com/ws/customers/" . $UN . "/contacts";


 


Notice the request is now operating over https.  I guess Dave missed that one..

Developer Portal

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

Visit Page