3 * StatusNet, the distributed open-source microblogging tool
5 * Class for doing HTTP basic auth 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 * General Exception wrapper for HTTP basic auth errors
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 BasicAuthException extends Exception
49 * Class for talking to the Twitter API with HTTP Basic Auth.
51 * @category Integration
53 * @author Zach Copley <zach@status.net>
54 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
55 * @link http://status.net/
58 class TwitterBasicAuthClient
60 var $screen_name = null;
66 * @param Foreign_link $flink a Foreign_link storing the
67 * Twitter user's password, etc.
69 function __construct($flink)
71 $fuser = $flink->getForeignUser();
72 $this->screen_name = $fuser->nickname;
73 $this->password = $flink->credentials;
77 * Calls Twitter's /statuses/update API method
79 * @param string $status text of the status
80 * @param mixed $params optional other parameters to pass to Twitter,
81 * as defined. For back-compatibility, if an int
82 * is passed we'll consider it a reply-to ID.
84 * @return mixed the status
86 function statusesUpdate($status, $in_reply_to_status_id = null)
88 $url = 'https://twitter.com/statuses/update.json';
89 if (is_numeric($params)) {
90 $params = array('in_reply_to_status_id' => intval($params));
92 $params['status'] = $status;
93 $params['source'] = common_config('integration', 'source');
94 $response = $this->httpRequest($url, $params);
95 $status = json_decode($response);
100 * Calls Twitter's /statuses/friends_timeline API method
102 * @param int $since_id show statuses after this id
103 * @param int $max_id show statuses before this id
104 * @param int $cnt number of statuses to show
105 * @param int $page page number
107 * @return mixed an array of statuses
109 function statusesFriendsTimeline($since_id = null, $max_id = null,
110 $cnt = null, $page = null)
112 $url = 'https://twitter.com/statuses/friends_timeline.json';
113 $params = array('since_id' => $since_id,
117 $qry = http_build_query($params);
123 $response = $this->httpRequest($url);
124 $statuses = json_decode($response);
129 * Calls Twitter's /statuses/home_timeline API method
131 * @param int $since_id show statuses after this id
132 * @param int $max_id show statuses before this id
133 * @param int $cnt number of statuses to show
134 * @param int $page page number
136 * @return mixed an array of statuses similar to friends timeline but including retweets
138 function statusesHomeTimeline($since_id = null, $max_id = null,
139 $cnt = null, $page = null)
141 $url = 'https://twitter.com/statuses/home_timeline.json';
142 $params = array('since_id' => $since_id,
146 $qry = http_build_query($params);
152 $response = $this->httpRequest($url);
153 $statuses = json_decode($response);
158 * Calls Twitter's /statuses/friends API method
160 * @param int $id id of the user whom you wish to see friends of
161 * @param int $user_id numerical user id
162 * @param int $screen_name screen name
163 * @param int $page page number
165 * @return mixed an array of twitter users and their latest status
167 function statusesFriends($id = null, $user_id = null, $screen_name = null,
170 $url = "https://twitter.com/statuses/friends.json";
172 $params = array('id' => $id,
173 'user_id' => $user_id,
174 'screen_name' => $screen_name,
176 $qry = http_build_query($params);
182 $response = $this->httpRequest($url);
183 $friends = json_decode($response);
188 * Calls Twitter's /statuses/friends/ids API method
190 * @param int $id id of the user whom you wish to see friends of
191 * @param int $user_id numerical user id
192 * @param int $screen_name screen name
193 * @param int $page page number
195 * @return mixed a list of ids, 100 per page
197 function friendsIds($id = null, $user_id = null, $screen_name = null,
200 $url = "https://twitter.com/friends/ids.json";
202 $params = array('id' => $id,
203 'user_id' => $user_id,
204 'screen_name' => $screen_name,
206 $qry = http_build_query($params);
212 $response = $this->httpRequest($url);
213 $ids = json_decode($response);
218 * Make an HTTP request
220 * @param string $url Where to make the request
221 * @param array $params post parameters
223 * @return mixed the request
224 * @throws BasicAuthException
226 function httpRequest($url, $params = null, $auth = true)
228 $request = HTTPClient::start();
229 $request->setConfig(array(
230 'follow_redirects' => true,
231 'connect_timeout' => 120,
233 'ssl_verify_peer' => false,
234 'ssl_verify_host' => false
238 $request->setAuth($this->screen_name, $this->password);
241 if (isset($params)) {
242 // Twitter is strict about accepting invalid "Expect" headers
243 $headers = array('Expect:');
244 $response = $request->post($url, $headers, $params);
246 $response = $request->get($url);
249 $code = $response->getStatus();
251 if ($code < 200 || $code >= 400) {
252 throw new BasicAuthException($response->getBody(), $code);
255 return $response->getBody();