SAMPLE: PUT (Update) a Contact (PHP)

Regular Participant

SAMPLE: PUT (Update) a Contact (PHP)

Skeletal PHP Script to Update an Existing Contact via a PUT.

/*A very simple example on how to update a contact name using a PUT command. To run
this script, provide your own Constant Contact User Name (UN), Password (PW), API Key (Key)
and Contact ID (SubID). Then provide an appropriate set of corresponding Contact details
including EmailAddress and Contact List ID (or series of Contact List IDs). Valid Contact ID and
Contact List IDs must be determined via an API Call (not Shown) */

$UN = "yourconstantcontactusername";
$PW = "yourconstantcontactpassword";
$Key = "yourconstantcontactAPIKey";
$SubID ="77898" ;//REPLACE WITH A VALID CONTACT Id, Which you can retrieve with a GET

$entry = '<entry xmlns="">
<title type="text">Contact:</title>
<content type="application/vnd.ctct+xml">
<Contact xmlns="" id="'.$UN.'/contacts/'.$SubID.'">
<ContactList id="'.$UN.'/lists/7">

The XML above will change the First name and Last name to Joe6 Smith6 Need to make
sure you have listed all the lists the contact belongs to, in addition to any lists you would like
the Contact added to. If you leave a Contact List out, the Contact will be removed from that
Contact List. This is format of Contact List XML entries:
<ContactList id="'.$UN.'/lists/7">
<ContactList id="'.$UN.'/lists/55">
<ContactList id="'.$UN.'/lists/57">

// create a temp file and load the xml in to it.
$tmpfile = tmpfile();
fwrite($tmpfile, $entry);
fseek($tmpfile, 0);

// Initialize the curl session
$request ="".$UN."/contacts/".$SubID;
$session = curl_init($request);

// Set up Digest authentication. Put your API key, and CTCT username/pwd here
$userNamePassword = $Key . '%' . $UN . ':' . $PW ;

curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);
curl_setopt($session, CURLOPT_INFILE, $tmpfile);
fclose($tmpfile); // Close temp file because it no longer needed

curl_setopt($session, CURLOPT_PUT, 1);
curl_setopt($session, CURLOPT_INFILESIZE, strlen($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, 0);
$response = curl_exec($session);
$httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);

if ($httpcode > 199 && $httpcode < 300)
echo "It works!!" ; // You might want GET the updated Contact and display it
{ echo "<br> There a Problem <br>Error Code: ". $httpcode ; }


Comments, additions and corrections encouraged!
Mike C, Constant Contact Support Engineer

Mike C Support Engineer, Constant Contact

Regular Participant

1) could you show what the function tmpfile() is?

right now i have         $fh = fopen('c:\\TEMP\\tmp.txt','rw');

which for some reason im getting

Warning: fopen(c:\TEMP\tmp.txt) : failed to open stream: No such file or directory in

i dont know why cuase im looking at the file


2) could you guys figure out some other way to do this

without littering my users harddrives with random files



Sorry for the missing code for the PUT request in php.  I will speak with Mike today and have him put up the code for his function as well.


Regarding part 2, this is a limitation of the PHP Curl feature.  For more information on how PHP Curl works, please see the official Curl documentation here.  If you want to use simpler methods for PUTing and POSTing, you would have to use a different language or find a PHP library that will augment standard PHP with a more REST focused library set.

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

What is $SubID  ?

What value I shall put in $SubID?

Where do I get such value to put into $SubID?



I got the following error mesage when call:

$response = curl_exec($session);


Error 415: Media Type Not Supported


I am very new.

Can any one help me?




$SubID is for a specific contact's ID.  Our database stores each email address (contact) in an account by a unique ID.  To get the contact ID for any given contact, you should do a GET for a contact.  That will return the contact's ID.


This is a link on how to retrieve a contact's information by their email address.


I hope that helps!  


Best Regards,

Shannon W.

API Support Specialist

Developer Portal

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

Visit Page