3 * StatusNet, the distributed open-source microblogging tool
5 * Class for doing OAuth calls against Twitter
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * @category Integration
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2009-2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * Class for talking to the Twitter API with OAuth.
37 * @category Integration
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
44 class TwitterOAuthClient extends OAuthClient
46 public static $requestTokenURL = 'https://api.twitter.com/oauth/request_token';
47 public static $authorizeURL = 'https://api.twitter.com/oauth/authorize';
48 public static $signinUrl = 'https://api.twitter.com/oauth/authenticate';
49 public static $accessTokenURL = 'https://api.twitter.com/oauth/access_token';
54 * @param string $oauth_token the user's token
55 * @param string $oauth_token_secret the user's token secret
59 function __construct($oauth_token = null, $oauth_token_secret = null)
61 $consumer_key = common_config('twitter', 'consumer_key');
62 $consumer_secret = common_config('twitter', 'consumer_secret');
64 if (empty($consumer_key) && empty($consumer_secret)) {
65 $consumer_key = common_config(
69 $consumer_secret = common_config(
71 'global_consumer_secret'
83 // XXX: the following two functions are to support the horrible hack
84 // of using the credentils field in Foreign_link to store both
85 // the access token and token secret. This hack should go away with
86 // 0.9, in which we can make DB changes and add a new column for the
89 static function packToken($token)
91 return implode(chr(0), array($token->key, $token->secret));
94 static function unpackToken($str)
96 $vals = explode(chr(0), $str);
97 return new OAuthToken($vals[0], $vals[1]);
100 static function isPackedToken($str)
102 if (strpos($str, chr(0)) === false) {
110 * Gets a request token from Twitter
112 * @return OAuthToken $token the request token
114 function getTwitterRequestToken()
116 return parent::getRequestToken(
117 self::$requestTokenURL,
118 common_local_url('twitterauthorization')
123 * Builds a link to Twitter's endpoint for authorizing a request token
125 * @param OAuthToken $request_token token to authorize
129 function getTwitterAuthorizeLink($request_token, $signin = false)
131 $url = ($signin) ? self::$signinUrl : self::$authorizeURL;
133 return parent::getAuthorizeLink($url,
135 common_local_url('twitterauthorization'));
139 * Fetches an access token from Twitter
141 * @param string $verifier 1.0a verifier
143 * @return OAuthToken $token the access token
145 function getTwitterAccessToken($verifier = null)
147 return parent::getAccessToken(
148 self::$accessTokenURL,
154 * Calls Twitter's /account/verify_credentials API method
156 * @return mixed the Twitter user
158 function verifyCredentials()
160 $url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
161 $response = $this->oAuthGet($url);
162 $twitter_user = json_decode($response);
163 return $twitter_user;
167 * Calls Twitter's /statuses/update API method
169 * @param string $status text of the status
170 * @param mixed $params optional other parameters to pass to Twitter,
171 * as defined. For back-compatibility, if an int
172 * is passed we'll consider it a reply-to ID.
174 * @return mixed the status
176 function statusesUpdate($status, $params=array())
178 $url = 'https://api.twitter.com/1.1/statuses/update.json';
179 if (is_numeric($params)) {
180 $params = array('in_reply_to_status_id' => intval($params));
182 $params['status'] = $status;
183 // We don't have to pass 'source' as the oauth key is tied to an app.
185 $response = $this->oAuthPost($url, $params);
186 $status = json_decode($response);
191 * Calls Twitter's /statuses/home_timeline API method
193 * @param int $since_id show statuses after this id
194 * @param string $timelineUri timeline to poll statuses from
195 * @param int $max_id show statuses before this id
196 * @param int $cnt number of statuses to show
197 * @param int $page page number
199 * @return mixed an array of statuses
201 function statusesTimeline($since_id = null, $timelineUri = 'home_timeline',
202 $max_id = null, $cnt = 200, $page = null)
204 $url = 'https://api.twitter.com/1.1/statuses/'.$timelineUri.'.json';
206 $params = array('include_entities' => 'true',
207 'include_rts' => 'true');
209 if (!empty($since_id)) {
210 $params['since_id'] = $since_id;
212 if (!empty($max_id)) {
213 $params['max_id'] = $max_id;
216 $params['count'] = $cnt;
219 $params['page'] = $page;
222 $response = $this->oAuthGet($url, $params);
223 $statuses = json_decode($response);
228 * Calls Twitter's /statuses/friends API method
230 * @param int $id id of the user whom you wish to see friends of
231 * @param int $user_id numerical user id
232 * @param int $screen_name screen name
233 * @param int $page page number
235 * @return mixed an array of twitter users and their latest status
237 function statusesFriends($id = null, $user_id = null, $screen_name = null,
240 $url = "https://api.twitter.com/1.1/friends/list.json";
248 if (!empty($user_id)) {
249 $params['user_id'] = $user_id;
252 if (!empty($screen_name)) {
253 $params['screen_name'] = $screen_name;
257 $params['page'] = $page;
260 $response = $this->oAuthGet($url, $params);
261 $friends = json_decode($response);
266 * Calls Twitter's /statuses/friends/ids API method
268 * @param int $id id of the user whom you wish to see friends of
269 * @param int $user_id numerical user id
270 * @param int $screen_name screen name
271 * @param int $page page number
273 * @return mixed a list of ids, 100 per page
275 function friendsIds($id = null, $user_id = null, $screen_name = null,
278 $url = "https://api.twitter.com/1.1/friends/ids.json";
286 if (!empty($user_id)) {
287 $params['user_id'] = $user_id;
290 if (!empty($screen_name)) {
291 $params['screen_name'] = $screen_name;
295 $params['page'] = $page;
298 $response = $this->oAuthGet($url, $params);
299 $ids = json_decode($response);
304 * Calls Twitter's /statuses/retweet/id.json API method
306 * @param int $id id of the notice to retweet
308 * @return retweeted status
311 function statusesRetweet($id)
313 $url = "https://api.twitter.com/1.1/statuses/retweet/$id.json";
314 $response = $this->oAuthPost($url);
315 $status = json_decode($response);
320 * Calls Twitter's /favorites/create API method
322 * @param int $id ID of the status to favorite
324 * @return object faved status
327 function favoritesCreate($id)
329 $url = "https://api.twitter.com/1.1/favorites/create.json";
332 $response = $this->oAuthPost($url, $params);
333 $status = json_decode($response);
338 * Calls Twitter's /favorites/destroy API method
340 * @param int $id ID of the status to unfavorite
342 * @return object unfaved status
345 function favoritesDestroy($id)
347 $url = "https://api.twitter.com/1.1/favorites/destroy.json";
350 $response = $this->oAuthPost($url,$params);
351 $status = json_decode($response);
356 * Calls Twitter's /statuses/destroy API method
358 * @param int $id ID of the status to destroy
360 * @return object destroyed
363 function statusesDestroy($id)
365 $url = "https://api.twitter.com/1.1/statuses/destroy/$id.json";
366 $response = $this->oAuthPost($url);
367 $status = json_decode($response);