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

Highlighted
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
Highlighted
Participant

Two quick questions...

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
Highlighted
Moderator

RE: Two quick questions...

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
Highlighted
Participant

Drop downs

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
Highlighted
Moderator

RE: Drop downs

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
Highlighted
Developer

re: PHP Sample - Sign Up Form

Please see also this PHP Sample.

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

Is there a sample of the form that goes with this?

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

Changes Due to New Security Requirements?

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.

Highlighted
Moderator

This code sample does use the

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

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Okay, so just to make sure,

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?

Developer Portal

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

Constant Contact would like to hear from you

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured