This is a small examples to retrieve user time line data (user_timeline) from Twitter Api 1.1 based on Zend Framework (I am using Zend Framework 1.12.3 here).
There are two types of examples.
1. Widget Type
It is inconvenient to redirect to Twitter page to authorise in certain applications like a widget. To skip the oAuth authorisation procedure, you can generate an access token and an access token secret in advance, and access Twitter Api using Zend_Service_Twitter.
2. Normal oAuth Type
This is normal procedure. First, get a request token to Twitter, redirect a user to Twitter to authorise an application, and retrieve an access token. Based on the access token, retrieve user time line data using Zend_Oauth libraries.
- Download Zend Framework. I downloaded Zend Framework 1.12.3 Full.
- Read Twitter Api Documentation.
- Read Zend_Service_Twitter.
- Read Zend oAuth.
- widget.php
- _application/configs/app.ini
-
Log in Twitter and register your application.
-
Create your access token in Details tab. See here in details.
-
Once you create access token and access token secret, rename _application/configs/app-sample.ini to _application/configs/app.ini and copy and paste access token, access token secret, consumer key and consumer secret to oauth.accessToken, oauth.accessTokenSecret, oauth.consumerKey, and oauth.consumerSecret in app.ini.
-
Get the Twitter User ID you want to get the data from. If you don't know what your Twitter User ID is, access gettwitterid.com and find it out.
-
Copy your Twitter User ID and paste it to user_id in app.ini. Also, if you want to manage the number of entries you want to get at once, change count value in app.ini.
// Get Twitter Timeline Data $accessToken = new Zend_Oauth_Token_Access(); $accessToken->setToken($twitterIni->oauth->accessToken); $accessToken->setTokenSecret($twitterIni->oauth->accessTokenSecret); $twitter = new Zend_Service_Twitter(array( 'accessToken' => $accessToken, 'oauthOptions' => array( 'consumerKey' => $twitterIni->oauth->consumerKey, 'consumerSecret' => $twitterIni->oauth->consumerSecret ) )); $response = $twitter->statusesUserTimeline(array('user_id' => $twitterIni->userId, 'count' => $twitterIni->userId)); $twitterData = $response->toValue();
- request_token.php
- callback.php
- _application/configs/app.ini
-
Log in Twitter and register your application.
-
Rname _application/configs/app-sample.ini to _application/configs/app.ini and copy and paste your consumer key and consumer secret to oauth.consumerKey and oauth.consumerSecret in app.ini. (_application/configs/app.ini).
-
Assign a callback URL in the Settings Tab and copy and paste it to oauth.redirectUrl in app.ini. See here
-
Get the Twitter User ID you want to get the data from. If you don't know what your Twitter User ID is, access gettwitterid.com and find it out.
-
Copy your Twitter User ID and paste it to user_id in app.ini. Also, if you want to manage the number of entries you want to get at once, change count value in app.ini.
// Request Token $config = array( 'callbackUrl' => $twitterIni->oauth->redirectUrl, 'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER, 'signatureMethod' => 'HMAC-SHA1', 'siteUrl' => $twitterIni->oauth->siteUrl, 'consumerKey' => $twitterIni->oauth->consumerKey, 'consumerSecret' => $twitterIni->oauth->consumerSecret ); $consumer = new Zend_Oauth_Consumer($config); $token = $consumer->getRequestToken(); $_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token); $consumer->redirect();
// Get Timeline Data $config = array( 'callbackUrl' => $twitterIni->oauth->redirectUrl, 'siteUrl' => $twitterIni->oauth->siteUrl, 'consumerKey' => $twitterIni->oauth->consumerKey, 'consumerSecret' => $twitterIni->oauth->consumerSecret ); $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']); $client = $token->getHttpClient($config); $client->setMethod(Zend_Http_Client::GET); $client->setUri('https://api.twitter.com/1.1/statuses/user_timeline.json'); $client->setParameterGet('user_id', $twitterIni->userId); $response = $client->request(); if($response->isSuccessful()) { if(strlen($response->getBody()) > 0) { $twitterData = Zend_Json::decode($response->getBody()); // Assign this variable to view or filter for something. Zend_Debug::dump($twitterData);exit; } }