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 * Exception wrapper for cURL 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 BasicAuthCurlException 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 a HTTP request using cURL.
188 * @param string $url Where to make the request
189 * @param array $params post parameters
191 * @return mixed the request
193 function httpRequest($url, $params = null, $auth = true)
196 CURLOPT_RETURNTRANSFER => true,
197 CURLOPT_FAILONERROR => true,
198 CURLOPT_HEADER => false,
199 CURLOPT_FOLLOWLOCATION => true,
200 CURLOPT_USERAGENT => 'StatusNet',
201 CURLOPT_CONNECTTIMEOUT => 120,
202 CURLOPT_TIMEOUT => 120,
203 CURLOPT_HTTPAUTH => CURLAUTH_ANY,
204 CURLOPT_SSL_VERIFYPEER => false,
206 // Twitter is strict about accepting invalid "Expect" headers
208 CURLOPT_HTTPHEADER => array('Expect:')
211 if (isset($params)) {
212 $options[CURLOPT_POST] = true;
213 $options[CURLOPT_POSTFIELDS] = $params;
217 $options[CURLOPT_USERPWD] = $this->screen_name .
218 ':' . $this->password;
221 $ch = curl_init($url);
222 curl_setopt_array($ch, $options);
223 $response = curl_exec($ch);
225 if ($response === false) {
226 $msg = curl_error($ch);
227 $code = curl_errno($ch);
228 throw new BasicAuthCurlException($msg, $code);