Hi, can anyone out there help me determine where my XML is causing this problem? Here's the XML:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<link href="/ws/customers/[removed for security]/contacts/10048" rel="edit"></link>
<id>http://api.constantcontact.com/ws/customers/[removed for security]/contacts/10048</id>
<title type="text">Contact: [removed for security]</title>
<updated>2012-07-04T22:45:23.251Z</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/[removed for security]/contacts/10048">
<Status>Active</Status>
<EmailAddress>[removed for security]</EmailAddress>
<EmailType>HTML</EmailType>
<Name> </Name>
<FirstName></FirstName>
<MiddleName></MiddleName>
<LastName></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/[removed for security]/lists/1">
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/[removed for security]/lists/1" rel="self"></link>
<OptInSource>ACTION_BY_CONTACT</OptInSource>
<OptInTime>2012-07-04T22:45:23.285Z</OptInTime>
</ContactList>
<ContactList id="http://api.constantcontact.com/ws/customers/[removed for security]/lists/2">
<link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/[removed for security]/lists/2" rel="self"></link>
<OptInSource>ACTION_BY_CONTACT</OptInSource>
<OptInTime>2011-04-05T17:51:30.656Z</OptInTime>
</ContactList>
</ContactLists>
<Confirmed>true</Confirmed>
<InsertTime>2011-04-05T17:51:30.653Z</InsertTime>
<LastUpdateTime>2012-07-04T22:45:23.251Z</LastUpdateTime>
</Contact>
</content>
<source>
<id>http://api.constantcontact.com/ws/customers/[removed for security]/contacts</id>
<title type="text">Contacts for Customer: [removed for security]</title>
<link href="contacts"></link>
<link href="contacts" rel="self"></link>
<author>
<name>[removed for security]</name>
</author>
<updated>2012-07-05T02:47:03.753Z</updated>
</source>
</entry>
Any help is appreciated!
Solved! Go to Solution.
No, it is not. I've run many successful requests with and without it. I'm glad it's now working for you. Let us know if you have any other questions.
Cheers,
Hello,
I just used the XML you provided to successfully update a contact in a test account using a PUT request. Did you recieve the error with the XML while making a request to update this contact (say, to make changes or add list memberships to the contact record?
Hi Mark,
Yes, I was trying to update the contact ID in the XML using a PUT request.
Here's the request:
// Initialize the curl session
$request ="https://api.constantcontact.com/ws/customers/[removed for security]/contacts/10048";
$session = curl_init($request);
// Set up Digest authentication. Put your API key, and CTCT username/pwd here
curl_setopt($session, CURLOPT_USERPWD, "REDACT");
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_INFILE, $tmpfile);
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);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($session);
$httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);
curl_close($session);
$tmpfile is the file XML string.
Hello,
In your script, is $tmpfile a file handle for a temporary file, or is the XML string going directly into the $tmpfile string? If the latter, this typically doesn't work with PHP cURL PUT requests, and I confirmed our API will return the 400 error you repoted.
The XML you are using is fine, based on my testing, but your request code may need a little tweaking. I was able to get the request to fly in in my test account by first putting xml into the string $entry, and writing it to a temporary file ($tmpfile) using the tmpfile() function, so instead of a string variable, $tmpfile is actual the handle for the temporary file. For convenience, I also made a small change to how the CURLOPT_USERPWD is set up in your code, resulting in the following working example:
<?php $entry = 'THE_XML'; $tmpfile = tmpfile(); fwrite($tmpfile, $entry); fseek($tmpfile, 0); // Put your API Key, Username and Password here $auth = $API_Key."%".$username.":".$password; // Initialize the curl session $request ="https://api.constantcontact.com/ws/customers/{username}/contacts/{user ID}"; $session = curl_init($request); curl_setopt($session, CURLOPT_USERPWD, $auth); curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($session, CURLOPT_INFILE, $tmpfile); 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); curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($session); $httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE); curl_close($session); ?>
I hope this helps you get your PUT requests rolling.
Cheers,
Hi Mark,
Once I removed the
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
tag from befor the <entry> tag, everything worked swimmingly. Is that line required?
Thanks for all of your help.
Joanne
No, it is not. I've run many successful requests with and without it. I'm glad it's now working for you. Let us know if you have any other questions.
Cheers,