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 Adrian Lang <mail@adrianlang.de>
40 * @author Brenda Wallace <shiny@cpan.org>
41 * @author Craig Andrews <candrews@integralblue.com>
42 * @author Dan Moore <dan@moore.cx>
43 * @author Evan Prodromou <evan@status.net>
44 * @author mEDI <medi@milaro.net>
45 * @author Sarven Capadisli <csarven@status.net>
46 * @author Zach Copley <zach@status.net> * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
50 class BasicAuthCurlException extends Exception
55 * Class for talking to the Twitter API with HTTP Basic Auth.
57 * @category Integration
59 * @author Zach Copley <zach@status.net>
60 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
61 * @link http://status.net/
64 class TwitterBasicAuthClient
66 var $screen_name = null;
72 * @param Foreign_link $flink a Foreign_link storing the
73 * Twitter user's password, etc.
75 function __construct($flink)
77 $fuser = $flink->getForeignUser();
78 $this->screen_name = $fuser->nickname;
79 $this->password = $flink->credentials;
83 * Calls Twitter's /statuses/update API method
85 * @param string $status text of the status
86 * @param int $in_reply_to_status_id optional id of the status it's
89 * @return mixed the status
91 function statusesUpdate($status, $in_reply_to_status_id = null)
93 $url = 'https://twitter.com/statuses/update.json';
94 $params = array('status' => $status,
95 'source' => common_config('integration', 'source'),
96 'in_reply_to_status_id' => $in_reply_to_status_id);
97 $response = $this->httpRequest($url, $params);
98 $status = json_decode($response);
103 * Calls Twitter's /statuses/friends_timeline API method
105 * @param int $since_id show statuses after this id
106 * @param int $max_id show statuses before this id
107 * @param int $cnt number of statuses to show
108 * @param int $page page number
110 * @return mixed an array of statuses
112 function statusesFriendsTimeline($since_id = null, $max_id = null,
113 $cnt = null, $page = null)
115 $url = 'https://twitter.com/statuses/friends_timeline.json';
116 $params = array('since_id' => $since_id,
120 $qry = http_build_query($params);
126 $response = $this->httpRequest($url);
127 $statuses = json_decode($response);
132 * Calls Twitter's /statuses/friends API method
134 * @param int $id id of the user whom you wish to see friends of
135 * @param int $user_id numerical user id
136 * @param int $screen_name screen name
137 * @param int $page page number
139 * @return mixed an array of twitter users and their latest status
141 function statusesFriends($id = null, $user_id = null, $screen_name = null,
144 $url = "https://twitter.com/statuses/friends.json";
146 $params = array('id' => $id,
147 'user_id' => $user_id,
148 'screen_name' => $screen_name,
150 $qry = http_build_query($params);
156 $response = $this->httpRequest($url);
157 $friends = json_decode($response);
162 * Calls Twitter's /statuses/friends/ids API method
164 * @param int $id id of the user whom you wish to see friends of
165 * @param int $user_id numerical user id
166 * @param int $screen_name screen name
167 * @param int $page page number
169 * @return mixed a list of ids, 100 per page
171 function friendsIds($id = null, $user_id = null, $screen_name = null,
174 $url = "https://twitter.com/friends/ids.json";
176 $params = array('id' => $id,
177 'user_id' => $user_id,
178 'screen_name' => $screen_name,
180 $qry = http_build_query($params);
186 $response = $this->httpRequest($url);
187 $ids = json_decode($response);
192 * Make a HTTP request using cURL.
194 * @param string $url Where to make the request
195 * @param array $params post parameters
197 * @return mixed the request
199 function httpRequest($url, $params = null, $auth = true)
202 CURLOPT_RETURNTRANSFER => true,
203 CURLOPT_FAILONERROR => true,
204 CURLOPT_HEADER => false,
205 CURLOPT_FOLLOWLOCATION => true,
206 CURLOPT_USERAGENT => 'StatusNet',
207 CURLOPT_CONNECTTIMEOUT => 120,
208 CURLOPT_TIMEOUT => 120,
209 CURLOPT_HTTPAUTH => CURLAUTH_ANY,
210 CURLOPT_SSL_VERIFYPEER => false,
212 // Twitter is strict about accepting invalid "Expect" headers
214 CURLOPT_HTTPHEADER => array('Expect:')
217 if (isset($params)) {
218 $options[CURLOPT_POST] = true;
219 $options[CURLOPT_POSTFIELDS] = $params;
223 $options[CURLOPT_USERPWD] = $this->screen_name .
224 ':' . $this->password;
227 $ch = curl_init($url);
228 curl_setopt_array($ch, $options);
229 $response = curl_exec($ch);
231 if ($response === false) {
232 $msg = curl_error($ch);
233 $code = curl_errno($ch);
234 throw new BasicAuthCurlException($msg, $code);