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 int $in_reply_to_status_id optional id of the status it's
83 * @return mixed the status
85 function statusesUpdate($status, $in_reply_to_status_id = null)
87 $url = 'https://twitter.com/statuses/update.json';
88 $params = array('status' => $status,
89 'source' => common_config('integration', 'source'),
90 'in_reply_to_status_id' => $in_reply_to_status_id);
91 $response = $this->httpRequest($url, $params);
92 $status = json_decode($response);
97 * Calls Twitter's /statuses/friends_timeline API method
99 * @param int $since_id show statuses after this id
100 * @param int $max_id show statuses before this id
101 * @param int $cnt number of statuses to show
102 * @param int $page page number
104 * @return mixed an array of statuses
106 function statusesFriendsTimeline($since_id = null, $max_id = null,
107 $cnt = null, $page = null)
109 $url = 'https://twitter.com/statuses/friends_timeline.json';
110 $params = array('since_id' => $since_id,
114 $qry = http_build_query($params);
120 $response = $this->httpRequest($url);
121 $statuses = json_decode($response);
126 * Calls Twitter's /statuses/friends API method
128 * @param int $id id of the user whom you wish to see friends of
129 * @param int $user_id numerical user id
130 * @param int $screen_name screen name
131 * @param int $page page number
133 * @return mixed an array of twitter users and their latest status
135 function statusesFriends($id = null, $user_id = null, $screen_name = null,
138 $url = "https://twitter.com/statuses/friends.json";
140 $params = array('id' => $id,
141 'user_id' => $user_id,
142 'screen_name' => $screen_name,
144 $qry = http_build_query($params);
150 $response = $this->httpRequest($url);
151 $friends = json_decode($response);
156 * Calls Twitter's /statuses/friends/ids API method
158 * @param int $id id of the user whom you wish to see friends of
159 * @param int $user_id numerical user id
160 * @param int $screen_name screen name
161 * @param int $page page number
163 * @return mixed a list of ids, 100 per page
165 function friendsIds($id = null, $user_id = null, $screen_name = null,
168 $url = "https://twitter.com/friends/ids.json";
170 $params = array('id' => $id,
171 'user_id' => $user_id,
172 'screen_name' => $screen_name,
174 $qry = http_build_query($params);
180 $response = $this->httpRequest($url);
181 $ids = json_decode($response);
186 * Make an HTTP request
188 * @param string $url Where to make the request
189 * @param array $params post parameters
191 * @return mixed the request
192 * @throws BasicAuthException
194 function httpRequest($url, $params = null, $auth = true)
196 $request = HTTPClient::start();
197 $request->setConfig(array(
198 'follow_redirects' => true,
199 'connect_timeout' => 120,
201 'ssl_verify_peer' => false,
202 'ssl_verify_host' => false
206 $request->setAuth($this->screen_name, $this->password);
209 if (isset($params)) {
210 // Twitter is strict about accepting invalid "Expect" headers
211 $headers = array('Expect:');
212 $response = $request->post($url, $headers, $params);
214 $response = $request->get($url);
217 $code = $response->getStatus();
219 if ($code < 200 || $code >= 400) {
220 throw new BasicAuthException($response->getBody(), $code);
223 return $response->getBody();