Add Contact - What's wrong with this code?

Regular Participant

Add Contact - What's wrong with this code?

What am I doing wrong

 

    session_start();
                $username = '';
                $password = '';
                $apiKey = '';
                $consumerSecret = '';
                $Datastore = new CTCTDataStore();
$DatastoreUser = $Datastore->lookupUser($username);
$ConstantContact = new ConstantContact('basic', $apiKey, $username, $password);
$Contact = new Contact();
$Contact->emailAddress = $form_state['values']['mail'];
$Contact->lists = array($lists['lists'][7]->id);
$NewContact = $ConstantContact->addContact($Contact);

4 REPLIES 4
Employee

Hi,

 

It looks like you're trying to use our PHP wrapper.  You will need to include the appropriate wrapper files to accomplish the task, and get contact lists so that the wrapper can handle them.  Here is the minimum code to add a contact using our PHP wrapper.  Keep in mind that this will NOT handle existing contacts in the account--you will get a 409 if you try to add an email address that was already in the account.

 

<html>
<body>
	<H1>Sign up for our email list</H1>
	<form action="index.php" method="post">
		<input type="text" name="EMAIL" maxlength="50"> 
		<input type="submit" name="submit" value="Submit" />
	</form>
</body>
</html>
	

<?php
session_start ();
require_once 'ConstantContact.php';

$username = '';
$password = '';
$apikey = '';
	
$ConstantContact = new ConstantContact('basic', $apikey, $username, $password); //basic authentication

// Get potential contact lists
$lists = $ConstantContact->getLists();
// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50

//Map form inputs here
$emailAddress = $_POST["EMAIL"];

// 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;
//Set the opt in source
$contactObj->optInSource = "ACTION_BY_CONTACT";
// Create the Contact and DONE
$Contact = $ConstantContact->addContact($contactObj);

		
?>

 

 

Best Regards,

Shannon W.

API Support Specialist

Occasional Participant

This helps me too. But the line:

require_once 'ConstantContact.php';

suggests I need to have the file ConstantContact.php on my server.  Where would I get that from?

 

Hi,

 

You can download the main PHP wrapper for our V1 API here; the code above uses the older wrapper, which utilizes the ConstantContact file.  I would recommend using the V2 API, however.  There is a sample signup form here (in the examples folder of the new PHP library).

 

Best Regards,

Shannon W.

API Support Specialist

Occasional Participant

Thanks. That helped.

Developer Portal

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

Visit Page