dev-master
9999999-devA simple Class to interface with Twitter API v1.1 using OAuth, and return tweets as JSON via a get method. Uses RestService and twitteroauth
MIT
The Requires
by Mutant Labs
api php oauth rest restful twitter
Wallogit.com
2017 © Pedro Peláez
A simple Class to interface with Twitter API v1.1 using OAuth, and return tweets as JSON via a get method. Uses RestService and twitteroauth
A simple Class to interface with Twitter API v1.1 using OAuth, and return tweets as JSON via a get method. Uses RestService and twitteroauth, (*1)
Requires https://github.com/marcj/php-rest-service, (*2)
Install twitter-rest with Composer:, (*3)
Create a composer.json:, (*4)
{
"require": {
"mutantlabs/twitter-rest": "dev-master"
}
}
and run, (*5)
$ wget http://getcomposer.org/composer.phar $ php composer.phar install
Example:, (*6)
//apache .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
create config.php as follows:, (*7)
Set your config.php (uses twitteroauth from https://github.com/abraham/twitteroauth), (*8)
define('CONSUMER_KEY', 'CONSUMER_KEY_HERE');
define('CONSUMER_SECRET', 'CONSUMER_SECRET_HERE');
define('OAUTH_TOKEN', 'OAUTH_TOKEN_HERE');
define('OAUTH_TOKEN_SECRET', 'OAUTH_TOKEN_SECRET_HERE');
define('OAUTH_CALLBACK', 'http://example.com/twitteroauth/callback.php');
include the vendor/autoload.php to make the class in your script available., (*9)
include 'vendor/autoload.php';
Construct new TwitterRestAPI, (*10)
use TwitterRest\TwitterRestAPI;
require_once('config.php');
$twitterRestApi = new TwitterRestAPI();
Get some tweets, (*11)
$tweets = $twitterRestApi->getCachedUserStatus();
$arrTweets = array();
foreach($tweets as $key => $status) {
$arrTweets[$key] = array(
'created_at' => $status->created_at,
'text' => $status->text
);
}
echo json_encode($arrTweets);
Note - to use getCachedUserStatus() - you need to make your apache server the owner of twitter_result.data:, (*12)
sudo chown -R www-data:www-data twitter_result.data
use TwitterRest\TwitterRestAPI; require_once('config.php'); TwitterRestAPI::create('/') ->addGetRoute('', function(){ $twitterRestApi = new TwitterRestAPI(); $tweets = $twitterRestApi->getCachedUserStatus(); $arrTweets = array(); foreach($tweets as $key => $status) { $arrTweets[$key] = array( 'created_at' => $status->created_at, 'text' => $status->text ); } return $arrTweets; }) ->run();
Extended Auth flow taken from https://github.com/abraham/twitteroauth, (*13)
->addGetRoute('authenticate', function(){
//When a user lands on /authenticate we build a new TwitterOAuth object using the client credentials.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
//Using the built $connection object you will ask Twitter for temporary credentials. The oauth_callback value is required.
$temporary_credentials = $connection->getRequestToken(OAUTH_CALLBACK);
//Once we have temporary credentials the user has to go to Twitter and authorize the app to access and updates their data.
$redirect_url = $connection->getAuthorizeURL($temporary_credentials, FALSE);
return array($temporary_credentials,$redirect_url);
})
The user is now on twitter.com and may have to login. Once authenticated with Twitter they will will either have to click on allow/deny, or will be automatically redirected back to the callback. in this case its example.domain.com/success, (*14)
Once the user has returned to /success and allowed access we need to build a new TwitterOAuth object using the temporary credentials., (*15)
->addGetRoute('success', function(){
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_GET['oauth_token'],
$_GET['oauth_verifier']);
//Now we ask Twitter for long lasting token credentials. These are specific to the application and user and will act like password to make future requests.
$token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']);
Here we can from our token credentials back to the application (i'd suggest a more secure method of sending these than encoded JSON. but here for example):
return array($token_credentials);
})
From here with the token credentials we build a new TwitterOAuth object., (*16)
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token_credentials['oauth_token'], $token_credentials['oauth_token_secret']);
And make requests authenticated as the user:, (*17)
$status = $connection->post('statuses/update', array('status' => 'Text of status here', 'in_reply_to_status_id' => 123456));
A simple Class to interface with Twitter API v1.1 using OAuth, and return tweets as JSON via a get method. Uses RestService and twitteroauth
MIT
api php oauth rest restful twitter