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 * 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/friends API method
131 * @param int $id id of the user whom you wish to see friends of
132 * @param int $user_id numerical user id
133 * @param int $screen_name screen name
134 * @param int $page page number
136 * @return mixed an array of twitter users and their latest status
138 function statusesFriends($id = null, $user_id = null, $screen_name = null,
141 $url = "https://twitter.com/statuses/friends.json";
143 $params = array('id' => $id,
144 'user_id' => $user_id,
145 'screen_name' => $screen_name,
147 $qry = http_build_query($params);
153 $response = $this->httpRequest($url);
154 $friends = json_decode($response);
159 * Calls Twitter's /statuses/friends/ids API method
161 * @param int $id id of the user whom you wish to see friends of
162 * @param int $user_id numerical user id
163 * @param int $screen_name screen name
164 * @param int $page page number
166 * @return mixed a list of ids, 100 per page
168 function friendsIds($id = null, $user_id = null, $screen_name = null,
171 $url = "https://twitter.com/friends/ids.json";
173 $params = array('id' => $id,
174 'user_id' => $user_id,
175 'screen_name' => $screen_name,
177 $qry = http_build_query($params);
183 $response = $this->httpRequest($url);
184 $ids = json_decode($response);
189 * Make an HTTP request
191 * @param string $url Where to make the request
192 * @param array $params post parameters
194 * @return mixed the request
195 * @throws BasicAuthException
197 function httpRequest($url, $params = null, $auth = true)
199 $request = HTTPClient::start();
200 $request->setConfig(array(
201 'follow_redirects' => true,
202 'connect_timeout' => 120,
204 'ssl_verify_peer' => false,
205 'ssl_verify_host' => false
209 $request->setAuth($this->screen_name, $this->password);
212 if (isset($params)) {
213 // Twitter is strict about accepting invalid "Expect" headers
214 $headers = array('Expect:');
215 $response = $request->post($url, $headers, $params);
217 $response = $request->get($url);
220 $code = $response->getStatus();
222 if ($code < 200 || $code >= 400) {
223 throw new BasicAuthException($response->getBody(), $code);
226 return $response->getBody();