Hi,
I tried your sample add contact form and the cc_class.php
I changed only the three lines. username, password and api. But i am getting the following error "An error Occurred"
cc_class.php
<?php
/**
* Class that is using REST to communicate with ConstantContact server
* @author ConstantContact Dev Team
* @version 1.0.3
* @since 27.07.2009
*
*/
class ConstantContact {
//FROM HERE YOU MAY MODIFY YOUR CREDENTIALS
var $login = 'agriz1984'; //username for your account
var $password = '******'; //password for your account
var $apikey = '7c8e885c-xxxx-401b-xxxx-59d2f0f1xxxx'; // Api Key for your account.
var $curl_debug = false; //set this to true to see the response code returned by cURL
var $contact_lists = array(); // define which lists will be available for signup.
var $force_lists = false; // set this to true to take away the ability for users to select and unselect lists
var $show_contact_lists = true; // set this to false to hide the list name(s) on the signup form.
// Contact Lists will only be hidden if force_lists is set to true. This is to prevent available checkboxes form being hidden.
// YOUR BASIC CHANGES SHOULD END RIGHT HERE
// Usually, below this line shouldn't be done any modification.
var $requestLogin; //this contains full authentication string.
var $apiPath = 'https://api.constantcontact.com/ws/customers/'; //is used for server calls.
var $lastError = ''; // this variable will contain last error message (if any)
var $doNotIncludeLists = array('Removed', 'Do Not Mail', 'Active'); //define which lists shouldn't be returned.
var $actionBy = 'ACTION_BY_CUSTOMER'; // can be ACTION_BY_CUSTOMER or ACTION_BY_CONTACT
public function __construct() {
//when the object is getting initialized, the login string must be created as API_KEY%LOGIN:PASSWORD
$this->requestLogin = $this->apikey."%".$this->login.":".$this->password;
$this->apiPath = $this->apiPath.$this->login;
}
/**
* Recursive Method that retrieves all the Email Lists from ConstantContact.
*
* @param string $path
* @return array of lists
*/
public function getLists($path = '') {
$mailLists = array();
if ( empty($path)) {
$call = $this->apiPath.'/lists';
} else {
$call = $path;
}
$return = $this->doServerCall($call);
$parsedReturn = simplexml_load_string($return);
$call2 = '';
foreach ($parsedReturn->link as $item) {
$tmp = $item->Attributes();
$nextUrl = '';
if ((string) $tmp->rel == 'next') {
$nextUrl = (string) $tmp->href;
$arrTmp = explode($this->login, $nextUrl);
$nextUrl = $arrTmp;
$call2 = $this->apiPath.$nextUrl;
break;
}
}
foreach ($parsedReturn->entry as $item) {
if ($this->contact_lists ){
if (in_array((string) $item->title, $this->contact_lists)) {
$tmp = array();
$tmp = (string) $item->id;
$tmp = (string) $item->title;
$mailLists[] = $tmp;
}
}
else if (!in_array((string) $item->title, $this->doNotIncludeLists)) {
$tmp = array();
$tmp = (string) $item->id;
$tmp = (string) $item->title;
$mailLists[] = $tmp;
}
}
if ( empty($call2)) {
return $mailLists;
} else {
return array_merge($mailLists, $this->getLists($call2));
}
}
public function subscriberExists($email = '') {
$call = $this->apiPath.'/contacts?email='.$email;
$return = $this->doServerCall($call);
$xml = simplexml_load_string($return);
$id = $xml->entry->id;
if($id){ return $id; }
else { return false; }
}
/**
* Method that retrieves from ConstantContact a collection with all the Subscribers
* If email parameter is mentioned then only mentioned contact is retrieved.
* @param string $email
* @return Bi-Dimenstional array with information about contacts.
*/
public function getSubscribers($email = '', $page = '') {
$contacts = array();
$contacts = array();
if (! empty($email)) {
$call = $this->apiPath.'/contacts?email='.$email;
} else {
if (! empty($page)) {
$call = $this->apiPath.$page;
} else {
$call = $this->apiPath.'/contacts';
}
}
$return = $this->doServerCall($call);
$parsedReturn = simplexml_load_string($return);
//we parse here the link array to establish which are the next page and previous page
foreach ($parsedReturn->link as $item) {
$attributes = $item->Attributes();
if (! empty($attributes) && $attributes == 'next') {
$tmp = explode($this->login, $attributes);
$contacts = $tmp;
}
if (! empty($attributes) && $attributes == 'first') {
$tmp = explode($this->login, $attributes);
$contacts = $tmp;
}
if (! empty($attributes) && $attributes == 'current') {
$tmp = explode($this->login, $attributes);
$contacts = $tmp;
}
}
foreach ($parsedReturn->entry as $item) {
$tmp = array();
$tmp = (string) $item->id;
$tmp = (string) $item->title;
$tmp = (string) $item->content->Contact->Status;
$tmp = (string) $item->content->Contact->EmailAddress;
$tmp = (string) $item->content->Contact->EmailType;
$tmp = (string) $item->content->Contact->Name;
$contacts[] = $tmp;
}
return $contacts;
}
/**
* Retrieves all the details for a specific contact identified by $email.
*
* @param string $email
* @return array with all information about the contact.
*/
public function getSubscriberDetails($email) {
$contact = $this->getSubscribers($email);
$fullContact = array();
$call = str_replace('http://', 'https://', $contact);
// Convert id URI to BASIC compliant
$return = $this->doServerCall($call);
$parsedReturn = simplexml_load_string($return);
$fullContact = $parsedReturn->id;
$fullContact = $parsedReturn->content->Contact->EmailAddress;
$fullContact = $parsedReturn->content->Contact->FirstName;
$fullContact = $parsedReturn->content->Contact->LastName;
$fullContact = $parsedReturn->content->Contact->MiddleName;
$fullContact = $parsedReturn->content->Contact->CompanyName;
$fullContact = $parsedReturn->content->Contact->JobTitle;
$fullContact = $parsedReturn->content->Contact->HomePhone;
$fullContact = $parsedReturn->content->Contact->WorkPhone;
$fullContact = $parsedReturn->content->Contact->Addr1;
$fullContact = $parsedReturn->content->Contact->Addr2;
$fullContact = $parsedReturn->content->Contact->Addr3;
$fullContact = (string) $parsedReturn->content->Contact->City;
$fullContact = (string) $parsedReturn->content->Contact->StateCode;
$fullContact = (string) $parsedReturn->content->Contact->StateName;
$fullContact = $parsedReturn->content->Contact->CountryCode;
$fullContact = $parsedReturn->content->Contact->PostalCode;
$fullContact = $parsedReturn->content->Contact->SubPostalCode;
$fullContact = $parsedReturn->content->Contact->CustomField1;
$fullContact = $parsedReturn->content->Contact->CustomField2;
$fullContact = $parsedReturn->content->Contact->CustomField3;
$fullContact = $parsedReturn->content->Contact->CustomField4;
$fullContact = $parsedReturn->content->Contact->CustomField5;
$fullContact = $parsedReturn->content->Contact->CustomField6;
$fullContact = $parsedReturn->content->Contact->CustomField7;
$fullContact = $parsedReturn->content->Contact->CustomField8;
$fullContact = $parsedReturn->content->Contact->CustomField9;
$fullContact = $parsedReturn->content->Contact->CustomField10;
$fullContact = $parsedReturn->content->Contact->CustomField11;
$fullContact = $parsedReturn->content->Contact->CustomField12;
$fullContact = $parsedReturn->content->Contact->CustomField13;
$fullContact = $parsedReturn->content->Contact->CustomField14;
$fullContact = $parsedReturn->content->Contact->CustomField15;
$fullContact = $parsedReturn->content->Contact->Note;
$fullContact = $parsedReturn->content->Contact->EmailType;
$fullContact = $parsedReturn->content->Contact->Status;
$fullContact = array();
if ($parsedReturn->content->Contact->ContactLists->ContactList) {
foreach ($parsedReturn->content->Contact->ContactLists->ContactList as $item) {
$fullContact[] = trim((string) $item->Attributes());
}
}
return $fullContact;
}
/**
* Method that modifies a contact State to DO NOT MAIL
*
* @param string $email - contact email address
* @return TRUE in case of success or FALSE otherwise
*/
public function deleteSubscriber($email) {
if ( empty($email)) {
return false;
}
$contact = $this->getSubscribers($email);
$id = $contact;
$return = $this->doServerCall($id, '', 'DELETE');
if (! empty($return)) {
return false;
}
return true;
}
/**
* Method that modifies a contact State to REMOVED
*
* @param string $email - contact email address
* @return TRUE in case of success or FALSE otherwise
*/
public function removeSubscriber($email) {
$contact = $this->getSubscriberDetails($email);
$contact = array();
$xml = $this->createContactXML($contact, $contact);
if ($this->editSubscriber($contact, $xml)) {
return true;
} else {
return false;
}
}
/**
* Upload a new contact to ConstantContact server
*
* @param strong $contactXML - formatted XML with contact information
* @return TRUE in case of success or FALSE otherwise
*/
public function addSubscriber($contactXML) {
$call = $this->apiPath.'/contacts';
$return = $this->doServerCall($call, $contactXML, 'POST');
$parsedReturn = simplexml_load_string($return);
if ($return) {
return true;
} else {
echo $xml = simplexml_load_string($contactXML);
$emailAddress = $xml->content->Contact->EmailAddress;
if ($this->subscriberExists($emailAddress)){
$this->lastError = 'This contact already exists. <a href="edit_contact.php?email='.$emailAddress.'">Click here</a> to edit the contact details.';
} else { $this->lastError = 'An Error Occurred'; }
return false;
}
}
/**
* Modifies a contact
*
* @param string $contactUrl - identifies the url for the modified contact
* @param string $contactXML - formed XML with contact information
* @return TRUE in case of success or FALSE otherwise
*/
public function editSubscriber($contactUrl, $contactXML) {
$return = $this->doServerCall($contactUrl, $contactXML, 'PUT');
if (! empty($return)) {
if (strpos($return, '<') !== false) {
$parsedReturn = simplexml_load_string($return);
if (! empty($parsedReturn->message)) {
$this->lastError = $parsedReturn->message;
}
} else {
$this->lastError = $parsedReturn->message;
}
return false;
}
return true;
}
/**
* Method that compose the needed XML format for a contact
*
* @param string $id
* @param array $params
* @return Formed XML
*/
public function createContactXML($id, $params = array()) {
if ( empty($id)) {
$id = "urn:uuid:E8553C09F4xcvxCCC53F481214230867087";
}
$update_date = date("Y-m-d").'T'.date("H:i:s").'+01:00';
$xml_string = "<entry xmlns='http://www.w3.org/2005/Atom'></entry>";
$xml_object = simplexml_load_string($xml_string);
$title_node = $xml_object->addChild("title", htmlspecialchars("TitleNode"));
$updated_node = $xml_object->addChild("updated", htmlspecialchars($update_date));
$author_node = $xml_object->addChild("author");
$author_name = $author_node->addChild("name", htmlspecialchars("CTCT Samples"));
$id_node = $xml_object->addChild("id", $id);
$summary_node = $xml_object->addChild("summary", htmlspecialchars("Customer document"));
$summary_node->addAttribute("type", "text");
$content_node = $xml_object->addChild("content");
$content_node->addAttribute("type", "application/vnd.ctct+xml");
$contact_node = $content_node->addChild("Contact", htmlspecialchars("Customer document"));
$contact_node->addAttribute("xmlns", "http://ws.constantcontact.com/ns/1.0/");
$email_node = $contact_node->addChild("EmailAddress", urldecode(htmlspecialchars($params)));
$fname_node = $contact_node->addChild("FirstName", urldecode(htmlspecialchars($params)));
$lname_node = $contact_node->addChild("LastName", urldecode(htmlspecialchars($params)));
$lname_node = $contact_node->addChild("MiddleName", urldecode(htmlspecialchars($params)));
$lname_node = $contact_node->addChild("CompanyName", urldecode(htmlspecialchars($params)));
$lname_node = $contact_node->addChild("JobTitle", urldecode(htmlspecialchars($params)));
//if ($params == 'Do Not Mail') {
$this->actionBy = 'ACTION_BY_CONTACT';
//}
$optin_node = $contact_node->addChild("OptInSource", htmlspecialchars($this->actionBy));
$hn_node = $contact_node->addChild("HomePhone", htmlspecialchars($params));
$wn_node = $contact_node->addChild("WorkPhone", htmlspecialchars($params));
$ad1_node = $contact_node->addChild("Addr1", htmlspecialchars($params));
$ad2_node = $contact_node->addChild("Addr2", htmlspecialchars($params));
$ad3_node = $contact_node->addChild("Addr3", htmlspecialchars($params));
$city_node = $contact_node->addChild("City", htmlspecialchars($params));
$state_node = $contact_node->addChild("StateCode", htmlspecialchars($params));
$state_name = $contact_node->addChild("StateName", htmlspecialchars($params));
$ctry_node = $contact_node->addChild("CountryCode", htmlspecialchars($params));
$zip_node = $contact_node->addChild("PostalCode", htmlspecialchars($params));
$subzip_node = $contact_node->addChild("SubPostalCode", htmlspecialchars($params));
$note_node = $contact_node->addChild("Note", htmlspecialchars($params));
$emailtype_node = $contact_node->addChild("EmailType", htmlspecialchars($params));
if (! empty($params)) {
foreach ($params as $k=>$v) {
$contact_node->addChild("CustomField".$k, htmlspecialchars($v));
}
}
$contactlists_node = $contact_node->addChild("ContactLists");
if ($params) {
foreach ($params as $tmp) {
$contactlist_node = $contactlists_node->addChild("ContactList");
$contactlist_node->addAttribute("id", $tmp);
}
}
$entry = $xml_object->asXML();
return $entry;
}
/**
* Method that returns a list with all states found on states.txt file
*
* @return array with state codes and state names
*/
public function getStates() {
$returnArr = array();
$lines = file("states.txt");
foreach ($lines as $line) {
$tmp = explode(" - ", $line);
if (sizeof($tmp) == 2) {
$returnArr)] = trim($tmp);
}
}
return $returnArr;
}
/**
* Returns a list with all countries found on countries.txt file
*
* @return array with country codes and country names
*/
public function getCountries() {
$returnArr = array();
$lines = file("countries.txt");
foreach ($lines as $line) {
$tmp = explode(" - ", $line);
if (sizeof($tmp) == 2) {
$returnArr)] = trim($tmp);
}
}
return $returnArr;
}
/**
* Private function used to send requests to ConstantContact server
*
* @param string $request - is the URL where the request will be made
* @param string $parameter - if it is not empty then this parameter will be sent using POST method
* @param string $type - GET/POST/PUT/DELETE
* @return a string containing server output/response
*/
private function doServerCall($request, $parameter = '', $type = "GET") {
$ch = curl_init();
$request = str_replace('http://', 'https://', $request);
// Convert id URI to BASIC compliant
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->requestLogin);
// curl_setopt ($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
switch ($type) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
break;
case 'PUT':
$tmpfile = tmpfile();
fwrite($tmpfile, $parameter);
fseek($tmpfile, 0);
curl_setopt($ch, CURLOPT_INFILE, $tmpfile);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($parameter));
fclose($tmpfile);
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
curl_setopt($ch, CURLOPT_HTTPGET, 1);
break;
}
$emessage = curl_exec($ch);
if ($this->curl_debug) {
echo $error = curl_error($ch);
}
curl_close($ch);
return $emessage;
}
}
?>
add_contact.php
<?php
//error_reporting(0);
include_once('cc_class.php');
$ccOBJ = new ConstantContact();
if (!empty($_POST)) {
$postFields = array();
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields= $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
// The Code is looking for a State Code For Example TX instead of Texas
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = $_POST;
$postFields = array();
foreach($_POST as $key=>$val) {
if (strncmp($key, 'custom_field_', strlen('custom_field_')) === 0) {
$postFields = $val;
}
}
$contactXML = $ccOBJ->createContactXML(null,$postFields);
if (!$ccOBJ->addSubscriber($contactXML)) {
$error = true;
} else {
$error = false;
$_POST = array();
}
}
//get all available lists for the current CC account.
$allLists = $ccOBJ->getLists();
//get all available states
$allStates = $ccOBJ->getStates();
//get all available countries
$allCountries = $ccOBJ->getCountries();
?>
I am just getting "An error Occurred". Kindly help me
... View more