cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a Contact

Occasional Visitor

Adding a Contact

Hello all,

 

  I am currently trying to integrate constant contact to the default Joomla registration page. I have used some of your PHP code but I'm running into trouble when sending the requests. I've probably made a simple error but I am somewhat new to PHP requests and any help would be greatly appreciated.

 

Thanks!

 

function onAfterRoute()
	{
		global $mainframe;
		$option = strtolower(strval(JRequest::getVar('option')));
		$task = strtolower(strval(JRequest::getVar('task')));
		
		// If not registering return
		if (!(($option == 'com_user') && ($task == 'register_save'))) return;
		
		$session = &JFactory::getSession();
		$name = strtolower(strval(JRequest::getVar('name')));
		$username = strtolower(strval(JRequest::getVar('username')));
		$email = strtolower(strval(JRequest::getVar('email')));
		$data = array($name, $username, $email);
		$session->set('tempregistrationdata', $data);
		
		// Add user to Constant Contact database
		if (JError::getError()) return;
		$subscribe = strtolower(strval(JRequest::getVar('subhidden', '')));
		if ($subscribe == "")
		{
			// Javascript disabled must use PHP
			$temp = strtolower(strval(JRequest::getVar('subscribe', '')));
			if ($temp == "yes") $subscribe = "true";
			elseif ($temp == "no") $subscribe = "false";
			else
			{
				JError::raiseWarning("666", "Please choose whether to subscribe to mailing lists.");
				$redirect = "index.php?option=com_user&view=register";
				JRequest::setVar('task', '');
				$mainframe->redirect($redirect);
				return;
			}
		}
		if ($subscribe == "true")
		{
			$lists = explode(',', $this->_params->get('ccLists', ''));
			// Cleanup whitespace
			foreach ($lists as &$item) $item = trim($item);
			$xml = $this->__generateCCXML($email, $lists);
			$url = "https://api.constantcontact.com/ws/customers/" . $this->_params->get("ccUsername", "") . "/contacts/";
			$result = $this->__httpCCRequest("PUT", $url, $xml);
		}	
	}
	
	function getCCHTML()
	{
		$html = 
		"<tr>
			<td height=\"40\">
				<label id=\"submsg\" for=\"subhidden\">
					Subscribe:
				</label>
			</td>
			<td>
				" . $this->_params->get("ccDescription", "Subscribe to mailing lists?") . "<br />
				<input type=\"radio\" name=\"subscribe\" id=\"subscribe0\" value=\"yes\" onclick=\"setHiddenValue(true)\" /> Yes
				<input type=\"radio\" name=\"subscribe\" id=\"subscribe1\" value=\"no\" onclick=\"setHiddenValue(false)\" /> No
				<input type=\"hidden\" name=\"subhidden\" id=\"subhidden\" value=\"\" class=\"inputbox required validate-subscribe\" />
			</td>
		</tr>\n";
		$html = str_replace("\n\t\t", "\n", $html); // Why not format html properly?
		return $html;
	}
	
	/**
	* Private function that creates XML for submission
	* MODIFIED: For Joomla plugin integration
	*
	* @author Constant Contact API Support Team <webservices@constantcontact.com> 
	* @version 1.0
	* @link http://developer.constantcontact.com
	*
	* @param string $email - email to add to database
	* @param string array $lists - array of strings of lists to add email to
	* @return string $data - valid XML of a list
	*/
	function __generateCCXML($email, $lists)
	{	
		global $mainframe;
		$xml = simplexml_load_string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><entry xmlns='http://www.w3.org/2005/Atom'></entry>");
		
		$id = "http://api.constantcontact.com/ws/customers/" . $this->_params->get("ccUsername", "");
		$id_node = $xml->addChild("id", ($id . "/contact"));
		
		$title = $xml->addChild("title", ("Contact: " . $email));
		$title->addAttribute("type", "text");
		
		$updated = date("Y-m-d") . 'T' . date("H:i:s") . '+01:00';
		$updated_node = $xml->addChild("updated", $updated);
		
		$author_node = $xml->addChild("author", $mainframe->getCfg('sitename'));
		
		$content = $xml->addChild("content");
		$content->addAttribute("type", "application/vnd.ctct+xml");
		
		$contact = $content->addChild("Contact");
		$contact->addAttribute("xmlns", "http://ws.constantcontact.com/ns/1.0/");
		$contact->addAttribute("id", ($id . "/contact"));
		
		$email_node = $contact->addChild("EmailAddress", $email);
		
		$optInSource = $contact->addChild("OptInSource", "ACTION_BY_CONTACT");
		
		$contactLists = $contact->addChild("ContactLists");
		foreach ($lists as $list)
		{
			$contactList = $contactLists->addChild("ContactList");
			$contactList->addAttribute("id", ($id . "/lists/" . $list));
		}
		
		$data = $xml->asXML();
		return $data;
	}
	/**
	* Private function httpCCRequest sends requests to the API server
	* MODIFIED: For Joomla plugin integration
	*
	* @param string $type - Server call, POST, GET, PUT, DELETE
	* @param string $url - valid constant contact URL
	* @param string $data - data passed to the URL
	* @return string array $result - returns an array from httpCCRequest with the API error and success messaging and codes
	*/
	function __httpCCRequest($type, $url, $data)
	{
		$apikey = $this->_params->get("ccApiKey", "");
		$username = $this->_params->get("ccUsername", "");
		$password = $this->_params->get("ccPassword", "");
		$auth = $apikey . "%" . $username . ":" . $password;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		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, $auth);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml", "accept:application/atom+xml", 'Content-Length: ' . strlen($data)));
		
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		
		$result = array();
		$result['xml'] = curl_exec($ch);
		$result['info'] = curl_getinfo($ch);
		$result['error'] = curl_error($ch);
		
		return $result;
	}

 

2 REPLIES 2
Occasional Visitor

Re: Adding a Contact

Could somebody please help me any advice would be greatly appreciated. I know that there must be a simple fix but for the life of me I can't see it. 

 

Thanks,

Android Smith

Highlighted
CTCT Employee

Re: Adding a Contact

Hi,

 

When you run this on your end, what error are you getting?

 

Also, what librar/wrapper are you using?  I do see that there are comments in it pointing towards being our library, but I do not recognize it.

 

Is it possible for you to send me the entire file here?

 

Thanks,

Benjamin Soder
NOC Analyst
Constant Contact