Skip to content

Instantly share code, notes, and snippets.

@kythin
Created June 25, 2012 23:12
Show Gist options
  • Save kythin/2992042 to your computer and use it in GitHub Desktop.
Save kythin/2992042 to your computer and use it in GitHub Desktop.
Twitter OAuth Quickstart
<?php
/*
* uses tmhOauth from https://github.com/themattharris/tmhOAuth/
* and is mostly based on the oauth flow example from that library (thanks matt!)
*
* also need your own twitter api key / secret, and make sure it's got write access
* if you want to publish posts (extra step when making your api key on twitter)
*
*
*/
//The setup... run these before anything else happens
require 'tmhOAuth.php';
require 'tmhUtilities.php';
$twitter = new tmhOAuth(array(
'consumer_key' => 'YOUR_CONSUMER_KEY',
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
));
//call this at the top of your page before <html>, or just your login page if you prefer.
//to start the login process, simply direct your user to "?page=login" (or change to suit).
if ($_GET['page']=='login') {
login_twitter_user();
}
//this is the function it runs on login, you will have to sort out some of the variables like SITE_BASE
//to log out, make a link to: yoursite.com/?wipe=true
define('SITE_BASE','http://www.yoursite.com/');
function login_twitter_user(){
global $twitter;
$here = tmhUtilities::php_self().'?page=login';
// reset request?
if ( isset($_REQUEST['wipe'])) {
session_destroy();
redirect(SITE_BASE);
// already got some credentials stored?
} elseif ( isset($_SESSION['access_token']) ) {
$twitter->config['user_token'] = $_SESSION['access_token']['oauth_token'];
$twitter->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
$code = $twitter->request('GET', $twitter->url('1/account/verify_credentials'));
if ($code == 200) {
$resp = json_decode($twitter->response['response']);
//echo $resp->screen_name;
//print_r($resp);
// ------------------------
// save_twitter_user($resp); //this is where you'd save the user to your database
// ------------------------
} else {
outputError($twitter);
}
// we're being called back by Twitter
} elseif (isset($_REQUEST['oauth_verifier'])) {
$twitter->config['user_token'] = $_SESSION['oauth']['oauth_token'];
$twitter->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
$code = $twitter->request('POST', $twitter->url('oauth/access_token', ''), array(
'oauth_verifier' => $_REQUEST['oauth_verifier']
));
if ($code == 200) {
$_SESSION['access_token'] = $twitter->extract_params($twitter->response['response']);
unset($_SESSION['oauth']);
header("Location: {$here}");
} else {
outputError($twitter);
}
// start the OAuth dance
} elseif ( isset($_REQUEST['authenticate']) || isset($_REQUEST['authorize']) ) {
$callback = isset($_REQUEST['oob']) ? 'oob' : $here;
$params = array(
'oauth_callback' => $callback
);
if (isset($_REQUEST['force_write'])) :
$params['x_auth_access_type'] = 'write';
elseif (isset($_REQUEST['force_read'])) :
$params['x_auth_access_type'] = 'read';
endif;
$code = $twitter->request('POST', $twitter->url('oauth/request_token', ''), $params);
if ($code == 200) {
$_SESSION['oauth'] = $twitter->extract_params($twitter->response['response']);
$method = isset($_REQUEST['authenticate']) ? 'authenticate' : 'authorize';
$force = isset($_REQUEST['force']) ? '&force_login=1' : '';
$authurl = $twitter->url("oauth/{$method}", '') . "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}";
//echo '<p>To complete the OAuth flow follow this URL: <a href="'. $authurl . '">' . $authurl . '</a></p>';
redirect($authurl);
} else {
outputError($twitter);
}
}
}
//simple error posting function used by the login function
function outputError($twitter) {
echo 'Error: ' . $twitter->response['response'] . PHP_EOL;
tmhUtilities::pr($twitter);
}
//simple redirect helper function
function redirect($page, $timeout = 0) {
if ($timeout) {
echo "<script type='text/javascript'>
<!--
function getgoing() {
top.location='$page';
}
setTimeout('getgoing()',".($timeout*1000).");
//-->
</script>";
} else {
echo "<script type='text/javascript'>
<!--
top.location = '$page'
//-->
</script>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment