What am I doing wrong
$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']->id);
$NewContact = $ConstantContact->addContact($Contact);
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']->id, $lists['lists']->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); ?>
API Support Specialist
This helps me too. But the line:
suggests I need to have the file ConstantContact.php on my server. Where would I get that from?
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).
API Support Specialist
View API documentation, code samples, get your API key.Visit Page
We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...Read More