cancel
Showing results for 
Search instead for 
Did you mean: 

Updating Contact from PHP

Valued Developer

Updating Contact from PHP

I use Adobe Flex to register contacts on my web site, it is connected to a PHP back - end which connects with the REST API. It works fine when creating new users. But I need the users to be able to select to which Contact List they want to subscribe. So when I changed the POST to a PUT I get an error.

I know there is something I am missing, but is the first time I use PUT and need some help.

Thanks in advance, heres the code:
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml'));
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);

curl_setopt($session, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($session, CURLOPT_PUT, 1);
curl_setopt($session, CURLOPT_POSTFIELDS , $content);
curl_setopt($session, CURLOPT_HEADER, 0); // Do not return headers
Tom M
Group Product Manager – Content Editing
Constant Contact
14 REPLIES 14
Occasional Participant

Updating Contact from PHP

Could you please post what the error is that you are getting? What's the error code and message (ie 415 - Unsupported Media Type)

Kevin
Occasional Participant

Another Problem

Well, I read again the Api Docs and I figured that the URI I need to use us the entry URI (contacts/1806) and not the one I was using the plain /contacts URI in my example

The new question I have is what is the best way to update a contact once I have its email. I figure I must send a query for the email address and then get its URI, update records and PUT to the specifi URI.

Is there an shorter way of doing it.

thanks
Valued Developer

Yes, get the URI, update XML and PUT

Yes, you have the right approach. Find the URI for the contact of interest, get that data, provide your updates and PUT it back.
Tom M
Group Product Manager – Content Editing
Constant Contact
Occasional Participant

I am having issues with this approach + bulk add

I have XML source that came from "contacts/4379". That user is subscribed to 2 lists (9 & 27) I remove the ContactList that corresponds to 27, and my XML looks like this:


<?xml version="1.0" ?>
<entry xmlns="http://www.w3.org/2005/Atom">
<link href="/ws/customers/USERNAME/contacts/4379" rel="edit"></link>
<id>http://api.constantcontact.com/ws/customers/USERNAME/contacts/4379</id>
<title type="text">Contact: tester1@jetboystudio.com</title>
<updated>2008-11-01T01:06:24.304Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/USERNAME/contacts/4379">
<Status>Active</Status>
<EmailAddress>tester1@jetboystudio.com</EmailAddress>
<EmailType>HTML</EmailType>
<Name>Fred Test</Name>
<FirstName>Fred</FirstName>
<MiddleName></MiddleName>
<LastName>Test</LastName>
<JobTitle></JobTitle>
<CompanyName></CompanyName>
<HomePhone></HomePhone>
<WorkPhone></WorkPhone>
<Addr1></Addr1>
<Addr2></Addr2>
<Addr3></Addr3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<CountryCode></CountryCode>
<CountryName></CountryName>
<PostalCode></PostalCode>
<SubPostalCode></SubPostalCode>
<Note></Note>
<CustomField1></CustomField1>
<CustomField2></CustomField2>
<CustomField3></CustomField3>
<CustomField4></CustomField4>
<CustomField5></CustomField5>
<CustomField6></CustomField6>
<CustomField7></CustomField7>
<CustomField8></CustomField8>
<CustomField9></CustomField9>
<CustomField10></CustomField10>
<CustomField11></CustomField11>
<CustomField12></CustomField12>
<CustomField13></CustomField13>
<CustomField14></CustomField14>
<CustomField15></CustomField15>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/USERNAME/lists/9">
<link xmlns="http://www.w3.org/2005/Atom" href="http://api.constantcontact.com/ws/customers/USERNAME/lists/9" rel="self"></link>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<OptInTime>2008-11-01T00:34:28.931Z</OptInTime>
</ContactList>
</ContactLists>
<Confirmed>false</Confirmed>
<InsertTime>2008-10-30T23:09:08.691Z</InsertTime>
<LastUpdateTime>2008-11-01T01:06:24.304Z</LastUpdateTime>
</Contact>
</content>
<source>
<id>http://api.constantcontact.com/ws/customers/USERNAME/contacts</id>
<title type="text">Contacts for Customer: USERNAME</title>
<link href="http://api.constantcontact.com/ws/customers/USERNAME/contacts"></link>
<link href="http://api.constantcontact.com/ws/customers/USERNAME/contacts" rel="self"></link>
<author>
<name>USERNAME</name>
</author>
<updated>2008-11-01T01:07:13.394Z</updated>
</source>
</entry>


I post this back to "contacts/4379" with this PHP ($user, $password, $apiKey, and $xml are filled correctly):

$url = "http://api.constantcontact.com/ws/customers/$user/contacts/4379";
print "POSTING TO $url";
$session = curl_init($url);
$userNamePassword = $apiKey . '%' . $user . ':' . $password;
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml'));
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);

curl_setopt($session, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS , $xml);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($session, CURLOPT_HEADER, 0);

$response = curl_exec($session);
curl_close($session);

if (!($xml = strstr($response, ' $xml = null;
echo "Error: $response";
}


Similar code worked great for all the other steps (get stuff: list lists, list contacts, get extended info on 1 contact) but it just hangs when I attempt to use it in POST to "contacts/4379". I get this (in XML error message):



Media Type Not Supported



If I use PUT, it says (after a long pause):


Request Too Large.




Also, when I bulk add users, they add, but it shows in my activity log that there are errors (which are "2 email address - Error - Invalid email address.") This only matters because it looks (to the client) like I am doing something wrong, even though it is actually adding them correctly (including the email address.) Can you explain why this happens? I'd be happy to send code, and responses back and forth, whatever would help.




By the way, I am the developer of the drupal module for CC. I am working on using the new API, rather then the old one, and am putting all the functions in an easy to use include, which I will make available for non-drupal users, too.



Also, your code filters are terrible. I can't use PRE, without full HTML, and had to convert all < and >. Try using this: http://drupal.org/project/geshifilter or at least http://drupal.org/project/codefilter

Valued Developer

re: Problem with Contact Update and Batch Upload

For the contact update (changing the set of lists), you should use PUT (update) not POST (create). I am not sure why you are getting the request too large issue. Can you try providing a minimal set of XML just as a test? (See for example the XML on updating a contact on this page). If that works we can build it back out to the full update XML you have and figure out if it is the XML or the API which has a problem.

The other issue you are seeing may be a defect. Is the message you posted above the exact error message, or does it say "Line 2..."?
Thanks,
Tom M
Group Product Manager – Content Editing
Constant Contact
Occasional Participant

Example of PUTing code

Tom, perhaps this is related to my question about my 401 Unauthorized error. Could you please post a complete example of PUTing a contact entry in PHP. The sample code doesn't include this functionality.
Occasional Participant

Code and output

Here is my code:
http://www.jetboystudio.com/downloads/cc/ConstantContact.phps



Here is the output (stripped of personal information):
http://www.jetboystudio.com/downloads/cc/output.txt



As far as I can tell, it looks fairly similar. I think a full example would help alot. Also, once the class is finished, you can offer it to your customers, as an example of how to use the service.



The full error is this:



Line Email Address Type Message
2 email address Error Invalid email address.


The problem with using the minimal XML example on that page, is that it doesn't actually contain a real user, so it doesn't help me to troubleshoot modifying a real user's subscriptions, which is what I'm trying to figure out, here. As I said, I can add users, and perform basically all user CRUD operations, except update. POST and GET seems to work ok (aside from the strange bulk update issues.) If the class seems hard to follow, just see the test code at the bottom. The main method for interacting with your server is ConstantContact->get() and I have centralized all interaction in that function (the rest is parsing and query building methods.) In researching curl PUTs, I found this: http://osterman.com/wordpress/2007/06/13/php-curl-put-string Could this be the problem (it shouldn't be POST data enclosed in a PUT, which curl just ignores, but rather PUT data from a file or string? - file for backward compatibility, because string are only supported in PHP 5.2)



Update:I'm on the right track, I think. I used these:


curl_setopt($session, CURLOPT_PUT, 4);
$fh = fopen('php://memory', 'rw');
fwrite($fh, $post);
rewind($fh);
curl_setopt($session, CURLOPT_INFILE, $fh);
curl_setopt($session, CURLOPT_INFILESIZE, strlen($post));


I'm using the memory object file, rather then a real file, for now, because my PHP is compatible, but I will need to switch later, for better compatibility.



It responded right away with this:


400: Contact List http://api.constantcontact.com/ws/customers/{USERNAME}/lists/9 does not exist



The list does exist, and the test email is a member of both 9 and 27. Is anyone using the new API? I can translate Python, Java, ruby, perl, or almost anything else if someone has an example of using PUTs with the API server.

Valued Developer

re: Code and Output (and PUT issue w/Contacts)

When a contact is PUT onto any list which is *not included* in the Account Owners SignUp Form, then the Contact Entry must include an OptInSource tag and the OptInSource must be ACTION_BY_CUSTOMER. (See the example XML on the Updating Contact|Adding or Removing a Contact from a List on this page. NOTE THE LOCATION OF THE OptInSource tag, outside of the ContactLists section.)



To determine if a Contact Lists is on the SignUp Form for the Account, see this point in the product: Contacts|Manage Contacts | List Properties to see if a Contact list is on the Signup Form).


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

OPT_IN_SOURCE

You'll notice in my output.txt that I am including that (XML near the bottom)