3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2008 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
35 * Class for talking to the Twitter API with OAuth.
37 * @category Integration
39 * @author Zach Copley <zach@controlyourself.ca>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://laconi.ca/
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);
68 * Builds a link to Twitter's endpoint for authorizing a request token
70 * @param OAuthToken $request_token token to authorize
74 function getAuthorizeLink($request_token)
76 return parent::getAuthorizeLink(self::$authorizeURL,
78 common_local_url('twitterauthorization'));
82 * Calls Twitter's /account/verify_credentials API method
84 * @return mixed the Twitter user
86 function verifyCredentials()
88 $url = 'https://twitter.com/account/verify_credentials.json';
89 $response = $this->oAuthGet($url);
90 $twitter_user = json_decode($response);
95 * Calls Twitter's /stutuses/update API method
97 * @param string $status text of the status
98 * @param int $in_reply_to_status_id optional id of the status it's
101 * @return mixed the status
103 function statusesUpdate($status, $in_reply_to_status_id = null)
105 $url = 'https://twitter.com/statuses/update.json';
106 $params = array('status' => $status,
107 'in_reply_to_status_id' => $in_reply_to_status_id);
108 $response = $this->oAuthPost($url, $params);
109 $status = json_decode($response);
114 * Calls Twitter's /stutuses/friends_timeline API method
116 * @param int $since_id show statuses after this id
117 * @param int $max_id show statuses before this id
118 * @param int $cnt number of statuses to show
119 * @param int $page page number
121 * @return mixed an array of statuses
123 function statusesFriendsTimeline($since_id = null, $max_id = null,
124 $cnt = null, $page = null)
127 $url = 'https://twitter.com/statuses/friends_timeline.json';
128 $params = array('since_id' => $since_id,
132 $qry = http_build_query($params);
138 $response = $this->oAuthGet($url);
139 $statuses = json_decode($response);
144 * Calls Twitter's /stutuses/friends API method
146 * @param int $id id of the user whom you wish to see friends of
147 * @param int $user_id numerical user id
148 * @param int $screen_name screen name
149 * @param int $page page number
151 * @return mixed an array of twitter users and their latest status
153 function statusesFriends($id = null, $user_id = null, $screen_name = null,
156 $url = "https://twitter.com/statuses/friends.json";
158 $params = array('id' => $id,
159 'user_id' => $user_id,
160 'screen_name' => $screen_name,
162 $qry = http_build_query($params);
168 $response = $this->oAuthGet($url);
169 $friends = json_decode($response);
174 * Calls Twitter's /stutuses/friends/ids API method
176 * @param int $id id of the user whom you wish to see friends of
177 * @param int $user_id numerical user id
178 * @param int $screen_name screen name
179 * @param int $page page number
181 * @return mixed a list of ids, 100 per page
183 function friendsIds($id = null, $user_id = null, $screen_name = null,
186 $url = "https://twitter.com/friends/ids.json";
188 $params = array('id' => $id,
189 'user_id' => $user_id,
190 'screen_name' => $screen_name,
192 $qry = http_build_query($params);
198 $response = $this->oAuthGet($url);
199 $ids = json_decode($response);