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 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://twitter.com/oauth/request_token';
47 public static $authorizeURL = 'https://twitter.com/oauth/authorize';
48 public static $accessTokenURL = 'https://twitter.com/oauth/access_token';
53 * @param string $oauth_token the user's token
54 * @param string $oauth_token_secret the user's token secret
58 function __construct($oauth_token = null, $oauth_token_secret = null)
60 $consumer_key = common_config('twitter', 'consumer_key');
61 $consumer_secret = common_config('twitter', 'consumer_secret');
63 parent::__construct($consumer_key, $consumer_secret,
64 $oauth_token, $oauth_token_secret);
67 // XXX: the following two functions are to support the horrible hack
68 // of using the credentils field in Foreign_link to store both
69 // the access token and token secret. This hack should go away with
70 // 0.9, in which we can make DB changes and add a new column for the
73 static function packToken($token)
75 return implode(chr(0), array($token->key, $token->secret));
78 static function unpackToken($str)
80 $vals = explode(chr(0), $str);
81 return new OAuthToken($vals[0], $vals[1]);
84 static function isPackedToken($str)
86 if (strpos($str, chr(0)) === false) {
94 * Builds a link to Twitter's endpoint for authorizing a request token
96 * @param OAuthToken $request_token token to authorize
100 function getAuthorizeLink($request_token)
102 return parent::getAuthorizeLink(self::$authorizeURL,
104 common_local_url('twitterauthorization'));
108 * Calls Twitter's /account/verify_credentials API method
110 * @return mixed the Twitter user
112 function verifyCredentials()
114 $url = 'https://twitter.com/account/verify_credentials.json';
115 $response = $this->oAuthGet($url);
116 $twitter_user = json_decode($response);
117 return $twitter_user;
121 * Calls Twitter's /statuses/update API method
123 * @param string $status text of the status
124 * @param int $in_reply_to_status_id optional id of the status it's
127 * @return mixed the status
129 function statusesUpdate($status, $in_reply_to_status_id = null)
131 $url = 'https://twitter.com/statuses/update.json';
132 $params = array('status' => $status,
133 'in_reply_to_status_id' => $in_reply_to_status_id);
134 $response = $this->oAuthPost($url, $params);
135 $status = json_decode($response);
140 * Calls Twitter's /statuses/friends_timeline API method
142 * @param int $since_id show statuses after this id
143 * @param int $max_id show statuses before this id
144 * @param int $cnt number of statuses to show
145 * @param int $page page number
147 * @return mixed an array of statuses
149 function statusesFriendsTimeline($since_id = null, $max_id = null,
150 $cnt = null, $page = null)
153 $url = 'https://twitter.com/statuses/friends_timeline.json';
154 $params = array('since_id' => $since_id,
158 $qry = http_build_query($params);
164 $response = $this->oAuthGet($url);
165 $statuses = json_decode($response);
170 * Calls Twitter's /statuses/friends API method
172 * @param int $id id of the user whom you wish to see friends of
173 * @param int $user_id numerical user id
174 * @param int $screen_name screen name
175 * @param int $page page number
177 * @return mixed an array of twitter users and their latest status
179 function statusesFriends($id = null, $user_id = null, $screen_name = null,
182 $url = "https://twitter.com/statuses/friends.json";
184 $params = array('id' => $id,
185 'user_id' => $user_id,
186 'screen_name' => $screen_name,
188 $qry = http_build_query($params);
194 $response = $this->oAuthGet($url);
195 $friends = json_decode($response);
200 * Calls Twitter's /statuses/friends/ids API method
202 * @param int $id id of the user whom you wish to see friends of
203 * @param int $user_id numerical user id
204 * @param int $screen_name screen name
205 * @param int $page page number
207 * @return mixed a list of ids, 100 per page
209 function friendsIds($id = null, $user_id = null, $screen_name = null,
212 $url = "https://twitter.com/friends/ids.json";
214 $params = array('id' => $id,
215 'user_id' => $user_id,
216 'screen_name' => $screen_name,
218 $qry = http_build_query($params);
224 $response = $this->oAuthGet($url);
225 $ids = json_decode($response);