Help with PHP add a contact...

Regular Participant

Help with PHP add a contact...

I am having some difficulty adding a contact using the php wrapper.  I am not a professional programmer and am very new to this.  We currently have formmail.php up and running and it seems to be pretty stable.  I wanted to manipulate some of the hidden fields in our forms as well as determine whether or not the person filling out the form wished to be added to the mailing list before submitting data to formmail.  I created a little file to do that for me.  Both forms on the website submit data to this script called "submit.php".  This works as long as the "mailing_list" checkbox is not checked.

Otherwise I get the error "This feed contains a DTD (Document Type Definition). DTDs are used to define a structure of a webpage. Internet Explorer does not support DTDs in feeds."


//Manipulate hidden fields for payment

if (isset($_POST["Registration_Selection"]) && $_POST["Registration_Selection"] == "Session 1 --- June 18 to June 23 2012 --- $325.00")
	$_POST["Tuition"] = "325";

if (isset($_POST["Registration_Selection"]) && $_POST["Registration_Selection"] == "Session 2 --- June 25 to June 30 2012 --- $325.00")
	$_POST["Tuition"] = "325";

if (isset($_POST["Registration_Selection"]) && $_POST["Registration_Selection"] == "Both Sessions --- June 18 to June 30 2012 --- $580.00")
	$_POST["Tuition"] = "580";

$_POST["Process_Fee"] = "10";

//Check to see if they want to join the mailing list and if they do include the script to add the email address

if (isset($_POST["Mailing_List"]) && $_POST["Mailing_List"] == "Yes")

// Now include FormMail to do all the rest of the work...


Here is the file "addcontact.php" that I am calling in the event that the checkbox is checked.  I found this in another post and I am just trying to get this bit working before trying to add the rest of the contact info to the contact object from the POST array.  I have place the php wrapper I downloaded into a folder called "ConstantConatct" and have removed my login info and key from the following.


	// Connecting to your account
	$ConstantContact = new ConstantContact("basic", "REMOVED", "REMOVED", "REMOVED");

	// Get potential contact lists
	$lists = $ConstantContact->getLists();
	// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50
	// Email address here is used for testing purposes
	// NOTE:  This email address will be added to your account if you execute this script,
	//		  you should send this to do-not-mail after your testing
	$emailAddress = "";
	// Search for our new Email address
	$search = $ConstantContact->searchContactsByEmail($emailAddress);
	// If the search didnt return a contact object
	if($search == false)
		// Create a new Contact Object to store data into
		$contactObj = new Contact();
		// Adding multiple lists to this new Contact Object
		$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
		// Set the email address
		$contactObj->emailAddress = $emailAddress;
		// Create the Contact and DONE
		$Contact = $ConstantContact->addContact($contactObj);
	} // Otherwise we update our existing
		// Gather data from our previous search and store it into a data type
		$contactObj = $ConstantContact->getContactDetails($search[0]);
		// We need to get the old list and add a new list to it as
		// this request requires a PUT and will remove the lists
		// as they are stored in an array
	     array_push($contactObj->lists, $lists['lists'][3]->id );
		// Update the contact and DONE
		$UpdateContact = $ConstantContact->updateContact($contactObj);



Any help would be greatly appreciated.


I tested the php code you provided that calls our API (using my own credentials, of course).  It appears to be working for me, and it should work for your account as well, as long as you have a list 3 in your account.


The DTD error specifically is an issue outside of Constant Contact, where the internet browser may not have the functionality to process certain scripts.  I know that is a major issue in Internet Explorer 7, so it would probably go away if you upgrade to IE8, or use another browser, like Firefox, Chrome, or Safari.


I hope that helps!

Regular Participant

I changed the following line:

array_push($contactObj->lists, $lists['lists'][3]->id );


array_push($contactObj->lists, $lists['lists'][0]->id, $lists['lists'][1]->id);

 Because I do not have a list 3.  Silly mistake.  I do have a list 1 and 2.  If I understand the code I am using correctly then


 refers to list number 1 and


 refers to the next list.


I now get the following error:

"Constant Contact HTTP Request Exception: Error 400: Invalid data: State and country values do not match."


Now I am completely confused...




That sounds like an issue with the XML that you are trying to pass into Constant Contact.  Typical XML to your account should look something like this:


<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="">
    <link href="/ws/customers/user_name/contacts/1" rel="edit"></link>
    <title type="text">Contact:</title>
        <name>Constant Contact</name>
    <content type="application/vnd.ctct+xml">
        <Contact xmlns="" id="">
            <Name>Shannon W</Name>
            <CustomField1>custom input 1</CustomField1>
            <CustomField2> </CustomField2>
            <CustomField4> </CustomField4>
            <CustomField5> </CustomField5>
                <ContactList id="">
                    <link xmlns="" href="/ws/customers/user_name/lists/3" rel="self"></link>
        <title type="text">Contacts for Customer: user_name</title>
        <link href="contacts"></link>
        <link href="contacts" rel="self"></link>


I would look at the XML that you are, in this instance, trying to pass into Constant Contact, and see if there is a discrepancy with the state and country you are trying to pass.  They can be empty, so your first troubleshooting step should probably be to delete the content inside the state and country values, and try again.  If that doesn't work, can you copy and paste your XML in your response?



Shannon W.

Constant Contact Support

Hi Joshua,


I'm also wondering, what values are you attempting to pass through the StateCode and CountryCode?

Nick Galbraith
Support Engineer

Hi all,


I'm also getting this error when using the PHP wrapper after retreiving a contact with getContactDetails and subsequently updating it with updateContact. The problem is that countryCode is  "  " after getContactDetails. Adding a trim countryCode solves the problem.


/Samuel Borgman

Developer Portal

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

Visit Page