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 $signinUrl = 'https://twitter.com/oauth/authenticate';
49 public static $accessTokenURL = 'https://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 parent::__construct($consumer_key, $consumer_secret,
65 $oauth_token, $oauth_token_secret);
68 // XXX: the following two functions are to support the horrible hack
69 // of using the credentils field in Foreign_link to store both
70 // the access token and token secret. This hack should go away with
71 // 0.9, in which we can make DB changes and add a new column for the
74 static function packToken($token)
76 return implode(chr(0), array($token->key, $token->secret));
79 static function unpackToken($str)
81 $vals = explode(chr(0), $str);
82 return new OAuthToken($vals[0], $vals[1]);
85 static function isPackedToken($str)
87 if (strpos($str, chr(0)) === false) {
95 * Builds a link to Twitter's endpoint for authorizing a request token
97 * @param OAuthToken $request_token token to authorize
101 function getAuthorizeLink($request_token, $signin = false)
103 $url = ($signin) ? self::$signinUrl : self::$authorizeURL;
105 return parent::getAuthorizeLink($url,
107 common_local_url('twitterauthorization'));
111 * Calls Twitter's /account/verify_credentials API method
113 * @return mixed the Twitter user
115 function verifyCredentials()
117 $url = 'https://twitter.com/account/verify_credentials.json';
118 $response = $this->oAuthGet($url);
119 $twitter_user = json_decode($response);
120 return $twitter_user;
124 * Calls Twitter's /statuses/update API method
126 * @param string $status text of the status
127 * @param int $in_reply_to_status_id optional id of the status it's
130 * @return mixed the status
132 function statusesUpdate($status, $in_reply_to_status_id = null)
134 $url = 'https://twitter.com/statuses/update.json';
135 $params = array('status' => $status,
136 'in_reply_to_status_id' => $in_reply_to_status_id);
137 $response = $this->oAuthPost($url, $params);
138 $status = json_decode($response);
143 * Calls Twitter's /statuses/friends_timeline API method
145 * @param int $since_id show statuses after this id
146 * @param int $max_id show statuses before this id
147 * @param int $cnt number of statuses to show
148 * @param int $page page number
150 * @return mixed an array of statuses
152 function statusesFriendsTimeline($since_id = null, $max_id = null,
153 $cnt = null, $page = null)
156 $url = 'https://twitter.com/statuses/friends_timeline.json';
157 $params = array('since_id' => $since_id,
161 $qry = http_build_query($params);
167 $response = $this->oAuthGet($url);
168 $statuses = json_decode($response);
173 * Calls Twitter's /statuses/friends API method
175 * @param int $id id of the user whom you wish to see friends of
176 * @param int $user_id numerical user id
177 * @param int $screen_name screen name
178 * @param int $page page number
180 * @return mixed an array of twitter users and their latest status
182 function statusesFriends($id = null, $user_id = null, $screen_name = null,
185 $url = "https://twitter.com/statuses/friends.json";
187 $params = array('id' => $id,
188 'user_id' => $user_id,
189 'screen_name' => $screen_name,
191 $qry = http_build_query($params);
197 $response = $this->oAuthGet($url);
198 $friends = json_decode($response);
203 * Calls Twitter's /statuses/friends/ids API method
205 * @param int $id id of the user whom you wish to see friends of
206 * @param int $user_id numerical user id
207 * @param int $screen_name screen name
208 * @param int $page page number
210 * @return mixed a list of ids, 100 per page
212 function friendsIds($id = null, $user_id = null, $screen_name = null,
215 $url = "https://twitter.com/friends/ids.json";
217 $params = array('id' => $id,
218 'user_id' => $user_id,
219 'screen_name' => $screen_name,
221 $qry = http_build_query($params);
227 $response = $this->oAuthGet($url);
228 $ids = json_decode($response);