<?php
/*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="http://www.w3.org/2005/Atom">
<id>http://api.constantcontact.com/ws/customers/'.$UN.'/contacts/'.$SubID.'</id>
<title type="text">Contact: test13420001000@3examghfghplexcvxc.com</title>
<updated>2008-04-25T19:29:06.096Z</updated>
<author><name>Mike</name></author>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/'.$UN.'/contacts/'.$SubID.'">
<EmailAddress>test13420001000@3examghfghplexcvxc.com</EmailAddress>
<FirstName>Joe6</FirstName>
<LastName>Smith6</LastName>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/'.$UN.'/lists/7">
</ContactList>
</ContactLists>
</Contact>
</content>
</entry>';
/*
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:
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/'.$UN.'/lists/7">
</ContactList>
<ContactList id="http://api.constantcontact.com/ws/customers/'.$UN.'/lists/55">
</ContactList>
<ContactList id="http://api.constantcontact.com/ws/customers/'.$UN.'/lists/57">
</ContactList>
</ContactLists>
*/
// create a temp file and load the xml in to it.
$tmpfile = tmpfile();
fwrite($tmpfile, $entry);
fseek($tmpfile, 0);
// Initialize the curl session
$request ="http://api.constantcontact.com/ws/customers/".$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_HTTPAUTH, CURLAUTH_DIGEST);
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);
curl_close($session);
if ($httpcode > 199 && $httpcode < 300)
{
echo "It works!!" ; // You might want GET the updated Contact and display it
}
else
{ echo "<br> There a Problem <br>Error Code: ". $httpcode ; }
?>
Mike C Support Engineer, Constant Contact
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.
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?
Thanks.
Hi,
$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
The holidays have come and gone. For many seasonal businesses, this means the rush of shoppers has decreased as well. Instead of turning off the lights and waiting for spring, make your email marketi...
See Article