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 * Class for talking to the Twitter API with HTTP Basic Auth.
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 TwitterBasicAuthClient
46 var $screen_name = null;
52 * @param Foreign_link $flink a Foreign_link storing the
53 * Twitter user's password, etc.
55 function __construct($flink)
57 $fuser = $flink->getForeignUser();
58 $this->screen_name = $fuser->nickname;
59 $this->password = $flink->credentials;
63 * Calls Twitter's /statuses/update API method
65 * @param string $status text of the status
66 * @param int $in_reply_to_status_id optional id of the status it's
69 * @return mixed the status
71 function statusesUpdate($status, $in_reply_to_status_id = null)
73 $url = 'https://twitter.com/statuses/update.json';
74 $params = array('status' => $status,
75 'source' => common_config('integration', 'source'),
76 'in_reply_to_status_id' => $in_reply_to_status_id);
77 $response = $this->httpRequest($url, $params);
78 $status = json_decode($response);
83 * Calls Twitter's /statuses/friends_timeline API method
85 * @param int $since_id show statuses after this id
86 * @param int $max_id show statuses before this id
87 * @param int $cnt number of statuses to show
88 * @param int $page page number
90 * @return mixed an array of statuses
92 function statusesFriendsTimeline($since_id = null, $max_id = null,
93 $cnt = null, $page = null)
95 $url = 'https://twitter.com/statuses/friends_timeline.json';
96 $params = array('since_id' => $since_id,
100 $qry = http_build_query($params);
106 $response = $this->httpRequest($url);
107 $statuses = json_decode($response);
112 * Calls Twitter's /statuses/friends API method
114 * @param int $id id of the user whom you wish to see friends of
115 * @param int $user_id numerical user id
116 * @param int $screen_name screen name
117 * @param int $page page number
119 * @return mixed an array of twitter users and their latest status
121 function statusesFriends($id = null, $user_id = null, $screen_name = null,
124 $url = "https://twitter.com/statuses/friends.json";
126 $params = array('id' => $id,
127 'user_id' => $user_id,
128 'screen_name' => $screen_name,
130 $qry = http_build_query($params);
136 $response = $this->httpRequest($url);
137 $friends = json_decode($response);
142 * Calls Twitter's /statuses/friends/ids API method
144 * @param int $id id of the user whom you wish to see friends of
145 * @param int $user_id numerical user id
146 * @param int $screen_name screen name
147 * @param int $page page number
149 * @return mixed a list of ids, 100 per page
151 function friendsIds($id = null, $user_id = null, $screen_name = null,
154 $url = "https://twitter.com/friends/ids.json";
156 $params = array('id' => $id,
157 'user_id' => $user_id,
158 'screen_name' => $screen_name,
160 $qry = http_build_query($params);
166 $response = $this->httpRequest($url);
167 $ids = json_decode($response);
172 * Make a HTTP request using cURL.
174 * @param string $url Where to make the request
175 * @param array $params post parameters
177 * @return mixed the request
179 function httpRequest($url, $params = null, $auth = true)
181 $request = HTTPClient::start();
182 $request->setConfig(array(
183 'follow_redirects' => true,
184 'connect_timeout' => 120,
186 'ssl_verify_peer' => false,
187 'ssl_verify_host' => false
191 $request->setAuth($this->screen_name, $this->password);
194 if (isset($params)) {
195 // Twitter is strict about accepting invalid "Expect" headers
196 $headers = array('Expect:');
197 $response = $request->post($url, $headers, $params);
199 $response = $request->get($url);
202 return $response->getBody();