<?php /** * * This config.php file is intended to hold you Constant Contact * Credentials. There are two different ways to implement the * oAuth2 authentication from with in this library. * * The first way is from with in session. This information is not * stored anywhere locally, other than that of the clients browser. * The following code breaks apart the session into the actual * credentials. The information could also be sent to a Database * or handled directly as you will see below. * */ $Datastore = new CTCTDataStore(); if (isset($_SESSION['users'])) { foreach ($_SESSION['users'] as $user) { if ($user['username'] != false) {
<?php header('Content-type: text/html'); // Start the Session and create a DataStore Object for storing this user. session_start(); require_once "ConstantContact.php"; $Datastore = new CTCTDataStore(); //Put your API Key, consumerSecret and RedirectURI here. $apiKey = ""; $consumerSecret = ""; $redirectURI = ""; $code = $_REQUEST['code']; $username = $_REQUEST['username']; //We will use PHP cURL to make a request to Constant Contact to get the Access Token. $rqurl = "https://oauth2.constantcontact.com/oauth2/oauth/token?grant_type=authorization_code&client_id={$apiKey}&client_secret={$consumerSecret}&code={$code}&redirect_uri={$redirectURI}"; $rq = curl_init(); curl_setopt($rq, CURLOPT_URL, $rqurl); curl_setopt($rq, CURLOPT_RETURNTRANSFER, 1); curl_setopt($rq, CURLOPT_BINARYTRANSFER, 1); curl_setopt($rq, CURLOPT_HEADER, 0); curl_setopt($rq, CURLOPT_SSL_VERIFYPEER, 0); if (!($result = curl_exec($rq))) { //If there is an error, dump it so we can see it. echo curl_error($rq); } else { echo "<pre>Access Token Information:\n"; print_r($result); $obj = json_decode($result); // The Result of this request is returned as JSON, so we need to parse it. //Create the User Ojbect. $user['username'] = $username;
public function makeRequest($url, $method, $body = '', $contentType = "application/atom+xml") { if ($this->authType == 'oauth') { $Datastore = new CTCTDataStore(); $accessInfo = $Datastore->lookupUser($this->username); $accessToken = new OAuthToken((string) $accessInfo['key'], (string) $accessInfo['secret']); $this->accessToken = $accessToken; $request = OAuthRequest::from_consumer_and_token($this->consumer, $accessToken, $method, $url); $request->sign_request($this->signatureMethod, $this->consumer, $accessToken); $request->get_normalized_http_method(); $url = $request; } $curl = curl_init(); if ($this->authType == 'basic') { curl_setopt($curl, CURLOPT_USERPWD, $this->requestLogin); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } else { if ($this->authType == 'oAuth2') { //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } } curl_setopt($curl, CURLOPT_URL, $url); //curl_setopt($curl, CURLOPT_FAILONERROR, 0); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); if ($this->authType == 'oauth2') { curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $this->access_token, "Content-Type: " . $contentType, "Content-Length: " . strlen($body), "Accept: application/atom+xml")); } else { curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: " . $contentType, "Content-Length: " . strlen($body), "Accept: application/atom+xml")); } curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); if ($body) { curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } try { $return['xml'] = curl_exec($curl); $return['info'] = curl_getinfo($curl); $return['error'] = curl_error($curl); curl_close($curl); if (!in_array($return['info']['http_code'], array('201', '200', '204'))) { throw new CTCTException('Constant Contact HTTP Request Exception: ' . $return['xml']); } else { $return = $return; } } catch (CTCTException $e) { $e->generateError(); } return $return; }
<h2>Add Contact Form Example</h2> <?php session_start(); include_once '../ConstantContact.php'; $username = '******'; $apiKey = 'APIKEY'; $consumerSecret = 'CONSUMERSECRET'; $Datastore = new CTCTDataStore(); $DatastoreUser = $Datastore->lookupUser($username); if ($DatastoreUser) { $ConstantContact = new ConstantContact('oauth', $apiKey, $DatastoreUser['username'], $consumerSecret); $ContactLists = $ConstantContact->getLists(); ?> <form name="addContact" action="" method="post"> Email Address: <input type="text" name="email_address"><br /> First Name: <input type="text" name="first_name"><br /> Last Name: <input type="text" name="last_name"><br /> <h4>Contact Lists</h4> <div style="border: 1px #000 solid; overflow: auto; width: 400px; height: 400px;"> <?php foreach ($ContactLists['lists'] as $list) { echo '<input type="checkbox" name="lists[]" value="' . $list->id . '"> ' . $list->name . '<br />'; } ?> </div> <input type="submit" name="submit" value="Add Contact"><br /> </form> <?php if (isset($_POST['email_address'])) { $Contact = new Contact();
<?php include_once '../ConstantContact.php'; session_start(); // Set variables $api_key = $_GET['apiKey']; // API Key $consumer_secret = $_GET['secret']; // Consumer Secret $_SESSION['return'] = $_GET['return']; $callback_url = 'http://' . $_SERVER['SERVER_NAME'] . ($_SERVER['SERVER_PORT'] != '80' ? ':' . $_SERVER['SERVER_PORT'] : '') . $_SERVER['REQUEST_URI']; // Instantiate ConstantContact class with the key and secret from ConstantContact.php $CTCTOAuth = new CTCTOAuth($api_key, $consumer_secret, $callback_url); if (!$_GET['oauth_verifier']) { $CTCTOAuth->getRequestToken(); $_SESSION['request_token'] = $CTCTOAuth->request_token->key; $_SESSION['request_secret'] = $CTCTOAuth->request_token->secret; header('Location: ' . $CTCTOAuth->generateAuthorizeUrl()); } else { $returnLocation = 'http://' . $_SESSION['return']; unset($_SESSION['return']); $requestToken = new OAuthToken($_SESSION['request_token'], $_SESSION['request_secret']); $CTCTOAuth->request_token = $requestToken; $CTCTOAuth->username = $_GET['username']; $CTCTOAuth->getAccessToken($_GET['oauth_verifier']); $sessionConsumer = array('key' => $CTCTOAuth->access_token->key, 'secret' => $CTCTOAuth->access_token->secret, 'username' => $CTCTOAuth->username); $Datastore = new CTCTDataStore(); $Datastore->addUser($sessionConsumer); header('Location: ' . $returnLocation); }
<?php session_start(); require_once 'ConstantContact.php'; require_once 'config.php'; $search = false; $action = false; // Set access code //$sessionConsumer = array ('username' => $_GET ["username"], 'access_token' => $_GET ["code"] ); $sessionConsumer = array('username' => $username, 'access_token' => $accessToken); $Datastore = new CTCTDataStore(); $Datastore->addUser($sessionConsumer); $username = $sessionConsumer['username']; $DatastoreUser = $Datastore->lookupUser($username); // Is there an active session with a username attached? If so we will allow them to search their contacts for an email address. if ($DatastoreUser) { ?> <html> <body> <?php $ConstantContact = new ConstantContact("oauth2", $apikey, $username, $accessToken); // This is to see if a post was made if ($_POST) { // print_r($_POST);exit; if ($_POST["action"]) { $action = true; } if ($_POST["action"] == "logout") { //If the user clicks on the Log Out button then we will end the session. session_destroy(); die("Logged Out, <a href='index.php'>Click Here</a> to continue.");