Skip to content
This repository
Newer
Older
100644 343 lines (279 sloc) 10.605 kb
6e7a8413 »
2008-11-05 Initial commit
1 <?php
2
3 /**
4 * A PHP implementation of the Twitter API. This implementation is based on
5 * the documentation found at
6 * <http://groups.google.com/group/twitter-development-talk/web/api-documentation>
7 * This implementation is _not_ fully tested, but it does definitely work
8 * for the functionality required by the EchoTwitBot and RssTwitBot.
9 *
10 * @author Neil Crosby, neil@twitbot.com
11 **/
12 class Twitter {
13
14 const OK = 200;
15 const NOT_MODIFIED = 304;
16 const BAD_REQUEST = 400;
17 const NOT_AUTHORIZED = 401;
18 const FORBIDDEN = 403;
19 const NOT_FOUND = 404;
20 const INTERNAL_SERVER_ERROR = 500;
21 const BAD_GATEWAY = 502;
22 const SERVICE_UNAVAILABLE = 503;
23
24 var $status = null;
25 var $user = null;
26 var $directMessages = null;
27 var $friendship = null;
28 var $account = null;
29 var $favorite = null;
30 var $notification = null;
31
32 function __construct( $username=null, $password=null, $apiPath="http://twitter.com/" ) {
33 if ( !$username || !$password || !$apiPath ) {
34 return;
35 }
36
37 $this->status = new TwitterStatus( $username, $password, $apiPath );
38 $this->user = new TwitterUser( $username, $password, $apiPath );
39 $this->directMessages = new TwitterDirectMessages( $username, $password, $apiPath );
40 $this->friendship = new TwitterFriendship( $username, $password, $apiPath );
41 $this->account = new TwitterAccount( $username, $password, $apiPath );
42 $this->favorite = new TwitterFavorite( $username, $password, $apiPath );
43 $this->notification = new TwitterNotification( $username, $password, $apiPath );
44 }
45 }
46
47 class TwitterBase {
48 function __construct( $username=null, $password=null, $apiPath=null ) {
49 if ( !$username || !$password || !$apiPath ) {
50 return;
51 }
52
53 $this->username = $username;
54 $this->password = $password;
55 $this->apiPath = $apiPath;
56 }
57
58 protected function getAsQueryString( $array = array() ) {
59
60 if ( !is_array($array) ) {
61 return '';
62 }
63
64 $items = array();
65
66 foreach ( $array as $key => $value ) {
67 array_push( $items, urlencode( $key ).'='.urlencode( $value ) );
68 }
69
70 $return = implode( '&', $items );
71 return $return;
72 }
73
74 protected function curl( $url, $aOptions = array() ) {
75 $session = curl_init();
76
77 curl_setopt( $session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
78 curl_setopt( $session, CURLOPT_USERPWD, $this->username . ":" . $this->password);
79 curl_setopt( $session, CURLOPT_URL, $this->apiPath . $url );
80 curl_setopt( $session, CURLOPT_HEADER, false );
81 curl_setopt( $session, CURLOPT_RETURNTRANSFER, 1 );
82
83 foreach ( $aOptions as $key => $value ) {
84 curl_setopt( $session, $key, $value );
85 }
86
87 $result = curl_exec( $session );
88
89 curl_close( $session );
90
91 if ( !isset($result) ) {
92 $result = false;
93 }
94
95 error_log($this->apiPath . $url);
96 error_log("HTTP CODE: ".curl_getinfo($session, CURLINFO_HTTP_CODE));
97
98 return $result;
99 }
100 }
101
102 class TwitterStatus extends TwitterBase {
103 function __construct( $username=null, $password=null, $apiPath=null ) {
104 parent::__construct( $username, $password, $apiPath );
105 }
106
107 public function public_timeline( $aOptions = array() ) {
108 // since_id
109 $queryString = $this->getAsQueryString( $aOptions );
110 $messages = $this->curl("statuses/public_timeline.json?$queryString");
111 return json_decode($messages);
112 }
113
114 public function friends_timeline( $aOptions = array() ) {
115 // id
116 // since
117 // page
118 $queryString = $this->getAsQueryString( $aOptions );
119 $messages = $this->curl("statuses/friends_timeline.json?$queryString");
120 return json_decode($messages);
121 }
122
123 public function user_timeline( $aOptions = array() ) {
124 // id
125 // count
126 // since
127 $queryString = $this->getAsQueryString( $aOptions );
128 $messages = $this->curl("statuses/user_timeline.json?$queryString");
129 return json_decode($messages);
130 }
131
132 public function show( $id ) {
133 $id = urlencode($id);
134 $messages = $this->curl("statuses/show/$id.json");
135 return json_decode($messages);
136 }
137
138 public function update( $status ) {
139 $status = urlencode( $status );
140
141 $this->curl(
142 "statuses/update.xml",
143 array(
144 CURLOPT_POST => 1,
145 CURLOPT_POSTFIELDS => "status=" . $status,
146 CURLOPT_TIMEOUT => 1,
147 )
148 );
149 }
150
151 public function replies( $aOptions = array() ) {
152 // page
153 $queryString = $this->getAsQueryString( $aOptions );
154 $messages = $this->curl("statuses/replies.json?$queryString");
155 return json_decode($messages);
156 }
157
158 public function destroy( $id ) {
159 $id = urlencode($id);
160 $messages = $this->curl("statuses/destroy/$id.json");
161 return json_decode($messages);
162 }
163
164 }
165
166 class TwitterUser extends TwitterBase {
167 function __construct( $username=null, $password=null, $apiPath=null ) {
168 parent::__construct( $username, $password, $apiPath );
169 }
170
171 public function friends( $aOptions = array() ) {
172 // id
173 // page
174 $queryString = $this->getAsQueryString( $aOptions );
175 $messages = $this->curl("statuses/friends.json?$queryString");
176 return json_decode($messages);
177 }
178
179 public function followers( $aOptions = array() ) {
180 // lite
181 $queryString = $this->getAsQueryString( $aOptions );
182 $messages = $this->curl("statuses/followers.json?$queryString");
183 return json_decode($messages);
184 }
185
186 public function featured() {
187 $messages = $this->curl("statuses/featured.json");
188 return json_decode($messages);
189 }
190
191 public function show( $id, $aOptions = array() ) {
192 // email
193 $id = urlencode($id);
194 $queryString = $this->getAsQueryString( $aOptions );
195 $messages = $this->curl("users/show/$id.json?$queryString");
196 return json_decode($messages);
197 }
198
199 }
200
201 class TwitterDirectMessages extends TwitterBase {
202 function __construct( $username=null, $password=null, $apiPath=null ) {
203 parent::__construct( $username, $password, $apiPath );
204 }
205
206 public function direct_messages( $aOptions = array() ) {
207 // since
208 // since_id
209 // page
210 $queryString = $this->getAsQueryString( $aOptions );
211 $messages = $this->curl("direct_messages.json?$queryString");
212 return json_decode($messages);
213 }
214
215 public function sent( $aOptions = array() ) {
216 // since
217 // since_id
218 // page
219 $queryString = $this->getAsQueryString( $aOptions );
220 $messages = $this->curl("direct_messages/sent.json?$queryString");
221 return json_decode($messages);
222 }
223
224 public function new_message( $user, $text ) {
225 $user = urlencode($user);
226 $text = urlencode($text);
227 echo "about to curl new message";
228 $this->curl(
229 "direct_messages/new.json",
230 array(
231 CURLOPT_POST => 1,
232 CURLOPT_POSTFIELDS => "user=$user&text=$text",
233 CURLOPT_TIMEOUT => 1,
234 )
235 );
236 }
237
238 public function destroy( $id ) {
239 $id = urlencode($id);
240 $messages = $this->curl("direct_messages/destroy/$id.json");
241 return json_decode($messages);
242 }
243
244 }
245
246 class TwitterFriendship extends TwitterBase {
247 function __construct( $username=null, $password=null, $apiPath=null ) {
248 parent::__construct( $username, $password, $apiPath );
249 }
250
251 public function create( $id ) {
252 $id = urlencode($id);
253 $friend = $this->curl("friendships/create/$id.json");
254 return json_decode($friend);
255 }
256
257 public function destroy( $id ) {
258 $id = urlencode($id);
259 $friend = $this->curl("friendships/destroy/$id.json");
260 return json_decode($friend);
261 }
262
263 }
264
265 class TwitterAccount extends TwitterBase {
266 function __construct( $username=null, $password=null, $apiPath=null ) {
267 parent::__construct( $username, $password, $apiPath );
268 }
269
270 public function verify_credentials() {
271 $messages = $this->curl("account/verify_creditials.json");
272 return json_decode($messages);
273 // TODO: need to do something a bit more special to see if this returned okay
274 }
275
276 public function end_session() {
277 $this->curl("account/end_session");
278 }
279
280 public function archive( $aOptions = array() ) {
281 $queryString = $this->getAsQueryString( $aOptions );
282 $messages = $this->curl("account/archive.json?$queryString");
283 return json_decode($messages);
284 }
285
286 }
287
288 class TwitterFavorite extends TwitterBase {
289 function __construct( $username=null, $password=null, $apiPath=null ) {
290 parent::__construct( $username, $password, $apiPath );
291 }
292
293 public function favorites( $aOptions = array() ) {
294 // The API definition of this function is weird=ass
295
296 // id
297 // page
298 $user = '';
299 if ( isset( $aOptions['id'] ) ) {
300 $user = '/'.urlencode($aOptions['id']);
301 $aOptions['id'] = null;
302 }
303
304 $queryString = $this->getAsQueryString( $aOptions );
305 $messages = $this->curl("favorites$user.json?$queryString");
306 return json_decode($messages);
307
308 }
309
310 public function create( $id ) {
311 $id = urlencode($id);
312 $messages = $this->curl("favorites/create/$id.json");
313 return json_decode($messages);
314 }
315
316 public function destroy( $id ) {
317 $id = urlencode($id);
318 $messages = $this->curl("favourites/destroy/$id.json");
319 return json_decode($messages);
320 }
321
322 }
323
324 class TwitterNotification extends TwitterBase {
325 function __construct( $username=null, $password=null, $apiPath=null ) {
326 parent::__construct( $username, $password, $apiPath );
327 }
328
329 public function follow( $id ) {
330 $id = urlencode($id);
331 $messages = $this->curl("notifications/follow/$id.json");
332 return json_decode($messages);
333 }
334
335 public function leave( $id ) {
336 $id = urlencode($id);
337 $messages = $this->curl("notifications/leave/$id.json");
338 return json_decode($messages);
339 }
340
341 }
342
343 ?>
Something went wrong with that request. Please try again.