public
Created

auth.php gets a request token, redirects to twitter for user authentication callback.php receives a callback from Twitter and saves the accessToken in the session update.php sends a tweet using a raw OAuth HTTP client (and DOES work) zst.php attempts t

  • Download Gist
auth.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
 
session_start();
 
set_include_path('../library');
require_once ('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
 
$config = array(
'callbackUrl' => 'http://' . $_SERVER['HTTP_HOST'] . '/callback.php',
'siteUrl' => 'http://twitter.com/oauth',
'consumerKey' => '5tsBnZBuuWP2FyqPlfDsVQ',
'consumerSecret' => ''
);
$consumer = new Zend_Oauth_Consumer($config);
 
$token = $consumer->getRequestToken();
// persist the token to storage
$_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token);
// redirect the user
$consumer->redirect();
callback.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
 
session_start();
 
set_include_path('../library');
require_once ('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
 
$config = array(
'callbackUrl' => 'http://' . $_SERVER['HTTP_HOST'] . '/callback.php',
'siteUrl' => 'http://twitter.com/oauth',
'consumerKey' => '5tsBnZBuuWP2FyqPlfDsVQ',
'consumerSecret' => ''
);
$consumer = new Zend_Oauth_Consumer($config);
 
if (!empty($_GET) && isset($_SESSION['TWITTER_REQUEST_TOKEN'])) {
$token = $consumer->getAccessToken(
$_GET,
unserialize($_SESSION['TWITTER_REQUEST_TOKEN'])
);
$_SESSION['TWITTER_ACCESS_TOKEN'] = serialize($token);
// Now that we have an Access Token, we can discard the Request Token
$_SESSION['TWITTER_REQUEST_TOKEN'] = null;
} else {
// Mistaken request? Some malfeasant trying something?
exit('Invalid callback request. Oops. Sorry.');
}
update.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
 
session_start();
 
set_include_path('../library');
require_once ('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
 
$config = array(
'callbackUrl' => 'http://' . $_SERVER['HTTP_HOST'] . '/callback.php',
'siteUrl' => 'http://twitter.com/oauth',
'consumerKey' => '5tsBnZBuuWP2FyqPlfDsVQ',
'consumerSecret' => ''
);
 
$statusMessage = !empty($_GET['msg']) ? $_GET['msg'] : 'I\'m posting to Twitter using Zend_Oauth!';
$token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
$client = $token->getHttpClient($config);
$client->setUri('http://twitter.com/statuses/update.json');
$client->setMethod(Zend_Http_Client::POST);
$client->setParameterPost('status', $statusMessage);
$response = $client->request();
$data = Zend_Json::decode($response->getBody());
$result = $response->getBody();
if (isset($data->text)) {
$result = 'true';
}
echo $result;
zst.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
 
session_start();
 
set_include_path('../library');
require_once ('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
 
$token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
Zend_Debug::dump($token);
$twitter = new Zend_Service_Twitter(array(
'username' => 'dcaunt',
'accessToken' => $token
));
// verify user's credentials with Twitter
//$response = $twitter->account->verifyCredentials();
 
$response = $twitter->status->update('MyGreatTweet');
 
Zend_Debug::dump($response);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.