Created
December 12, 2012 12:23
-
-
Save susanBuck/4267372 to your computer and use it in GitHub Desktop.
Twitter integration demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ADD THIS SOMEWHERE IN YOUR MASTER TEMPLATE (OR WHEREEVER YOU WANT IT TO APPEAR) --> | |
<? if (!$user->twitter->connected): ?> | |
You are <strong>not</strong> connected to Twitter. <a href='/twitter/redirect'>Would you like to connect?</a> | |
<? else: ?> | |
You are connected to Twitter. <a href='/twitter/clearsessions'>Disconnect</a> | |
<? endif; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function __construct() { | |
# Instantiate User obj | |
$this->userObj = new User(); | |
# Authenticate / load user | |
$this->user = $this->userObj->authenticate(); | |
# Set up templates | |
$this->template = View::instance('_v_template'); | |
$this->email_template = View::instance('_v_email'); | |
# So we can use $user in views | |
$this->template->set_global('user', $this->user); | |
# Is this user connected to Twitter? | |
if($this->user) { | |
# Ideally these constants should be in config.php but leaving here for demonstration purposes. | |
define('CONSUMER_KEY', 'DaCTGU1jK93WxrZ5RKg'); | |
define('CONSUMER_SECRET', 'H9DS2HoWBv8PEseeg2bpQj9Ho9A0lq3GJgqEPRvfZ8'); | |
# Create object in user object to store twitter related data | |
$this->user->twitter = new stdClass; | |
# First, find out if they're already conected | |
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) { | |
$this->user->twitter->connected = FALSE; | |
} | |
else { | |
$this->user->twitter->connected = TRUE; | |
# Get user access tokens out of the session. | |
$access_token = $_SESSION['access_token']; | |
# Create a TwitterOauth object with consumer/user tokens. | |
$this->user->twitter->connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); | |
} | |
} | |
# Pass that array to the template. | |
$this->template->navigation = $nav; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ADD THIS SOMEWHERE IN YOUR c_posts.php p_add() method | |
# [...code here about adding the post to the database...] | |
# Tweet it if they're connected | |
if($this->user->twitter->connected) { | |
$tweet = $this->user->twitter->connection->post('statuses/update', array('status' => $_POST['content'])); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// BRAND NEW TWITTER CONTROLLER | |
class twitter_controller extends base_controller { | |
public function __construct() { | |
parent::__construct(); | |
} | |
/*------------------------------------------------------------------------------------------------- | |
This is what talks to Twitter to create the connection | |
-------------------------------------------------------------------------------------------------*/ | |
public function redirect() { | |
# Build TwitterOAuth object with client credentials. | |
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); | |
# Get temporary credentials | |
$request_token = $connection->getRequestToken(APP_URL.'/twitter/callback'); | |
# Save temporary credentials to session. | |
$_SESSION['oauth_token'] = $token = $request_token['oauth_token']; | |
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; | |
# If last connection failed don't display authorization link. | |
switch ($connection->http_code) { | |
case 200: | |
# Build authorize URL and redirect user to Twitter. | |
$url = $connection->getAuthorizeURL($token); | |
header('Location: ' . $url); | |
break; | |
default: | |
echo 'Could not connect to Twitter. Refresh the page or try again later.'; | |
} | |
} | |
/*------------------------------------------------------------------------------------------------- | |
This is the URL Twitter hits after we hit it from redirect() | |
We specify above this in redirect() | |
-------------------------------------------------------------------------------------------------*/ | |
public function callback() { | |
# If the oauth_token is old redirect to the connect page | |
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) { | |
$_SESSION['oauth_status'] = 'oldtoken'; | |
header('Location: /clearsessions/'); | |
} | |
# Create TwitteroAuth object with app key/secret and token key/secret from default phase | |
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); | |
# Request access tokens from twitter | |
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); | |
# Save the access tokens. Normally these would be saved in a database for future use. | |
$_SESSION['access_token'] = $access_token; | |
# Remove no longer needed request tokens | |
unset($_SESSION['oauth_token']); | |
unset($_SESSION['oauth_token_secret']); | |
# If HTTP response is 200 continue otherwise send to connect page to retry | |
if (200 == $connection->http_code) { | |
# The user has been verified and the access tokens can be saved for future use | |
$_SESSION['status'] = 'verified'; | |
header('Location: /'); | |
} else { | |
# Save HTTP status for error dialog on connnect page. | |
header('Location: /twitter/clearsessions/'); | |
} | |
} | |
/*------------------------------------------------------------------------------------------------- | |
When we want to disconnect from Twitter | |
-------------------------------------------------------------------------------------------------*/ | |
public function clearsessions() { | |
/* Load and clear sessions */ | |
session_destroy(); | |
/* Redirect to page with the connect to Twitter option. */ | |
header('Location: /'); | |
} | |
} # eoc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment