| 1 |
<?php
|
| 2 |
// $Id: twitter.lib.php,v 1.1.2.3 2009/06/11 02:50:07 walkah Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Classes to implement the full Twitter API
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Exception handling class.
|
| 12 |
*/
|
| 13 |
class TwitterException extends Exception {
|
| 14 |
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Primary Twitter API implementation class
|
| 19 |
* Supports the full REST API for twitter.
|
| 20 |
*/
|
| 21 |
class Twitter {
|
| 22 |
|
| 23 |
/**
|
| 24 |
* @var $format API format to use: can be json or xml
|
| 25 |
*/
|
| 26 |
protected $format = 'json';
|
| 27 |
|
| 28 |
/**
|
| 29 |
* @var $host The host to query against
|
| 30 |
*/
|
| 31 |
protected $host = 'twitter.com';
|
| 32 |
|
| 33 |
/**
|
| 34 |
* @var $source the twitter api 'source'
|
| 35 |
*/
|
| 36 |
protected $source = 'drupal';
|
| 37 |
|
| 38 |
/**
|
| 39 |
* @var $username Twitter username to use for authenticated requests
|
| 40 |
*/
|
| 41 |
protected $username;
|
| 42 |
|
| 43 |
/**
|
| 44 |
* @var $password Twitter password to use for authenticated requests
|
| 45 |
*/
|
| 46 |
protected $password;
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Constructor for the Twitter class
|
| 50 |
*/
|
| 51 |
public function __construct($username = NULL, $password = NULL) {
|
| 52 |
if (!empty($username) && !empty($password)) {
|
| 53 |
$this->set_auth($username, $password);
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Set the username and password
|
| 59 |
*/
|
| 60 |
public function set_auth($username, $password) {
|
| 61 |
$this->username = $username;
|
| 62 |
$this->password = $password;
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Set the current host. This is good for using laconi.ca instances
|
| 67 |
* and anything else that utilizes the Twitter API
|
| 68 |
*
|
| 69 |
* @param $host the hostname to set.
|
| 70 |
*/
|
| 71 |
public function set_host($host) {
|
| 72 |
$this->host = $host;
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Get an array of TwitterStatus objects from an API endpoint
|
| 77 |
*/
|
| 78 |
protected function get_statuses($path, $params = array(), $use_auth = FALSE) {
|
| 79 |
$values = $this->call($path, $params, 'GET', $use_auth);
|
| 80 |
$statuses = array();
|
| 81 |
foreach ($values as $status) {
|
| 82 |
$statuses[] = new TwitterStatus($status);
|
| 83 |
}
|
| 84 |
return $statuses;
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Fetch the public timeline
|
| 89 |
*
|
| 90 |
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-public_timeline
|
| 91 |
*/
|
| 92 |
public function public_timeline() {
|
| 93 |
return $this->get_statuses('statuses/public_timeline');
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Fetch the authenticated user's friends timeline
|
| 98 |
*
|
| 99 |
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-friends_timeline
|
| 100 |
*/
|
| 101 |
public function friends_timeline($params = array()) {
|
| 102 |
return $this->get_statuses('statuses/friends_timeline', $params, TRUE);
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Fetch a user's timeline
|
| 107 |
*
|
| 108 |
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline
|
| 109 |
*/
|
| 110 |
public function user_timeline($id, $params = array(), $use_auth = FALSE) {
|
| 111 |
if (is_numeric($id)) {
|
| 112 |
$params['user_id'] = $id;
|
| 113 |
}
|
| 114 |
else {
|
| 115 |
$params['screen_name'] = $id;
|
| 116 |
}
|
| 117 |
|
| 118 |
return $this->get_statuses('statuses/user_timeline', $params, $use_auth);
|
| 119 |
}
|
| 120 |
|
| 121 |
/**
|
| 122 |
*
|
| 123 |
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-mentions
|
| 124 |
*/
|
| 125 |
public function mentions($params = array()) {
|
| 126 |
return $this->get_statuses('statuses/mentions', $params, TRUE);
|
| 127 |
}
|
| 128 |
|
| 129 |
/**
|
| 130 |
*
|
| 131 |
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0update
|
| 132 |
*/
|
| 133 |
public function status_update($status, $params = array()) {
|
| 134 |
$params['status'] = $status;
|
| 135 |
if ($this->source) {
|
| 136 |
$params['source'] = $this->source;
|
| 137 |
}
|
| 138 |
$values = $this->call('statuses/update', $params, 'POST', TRUE);
|
| 139 |
|
| 140 |
return new TwitterStatus($values);
|
| 141 |
}
|
| 142 |
|
| 143 |
|
| 144 |
/**
|
| 145 |
*
|
| 146 |
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show
|
| 147 |
*/
|
| 148 |
public function users_show($id) {
|
| 149 |
$params = array();
|
| 150 |
if (is_numeric($id)) {
|
| 151 |
$params['user_id'] = $id;
|
| 152 |
}
|
| 153 |
else {
|
| 154 |
$params['screen_name'] = $id;
|
| 155 |
}
|
| 156 |
|
| 157 |
$values = $this->call('users/show', $params, 'GET', TRUE);
|
| 158 |
return new TwitterUser($values);
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
*
|
| 163 |
* @see http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0verify_credentials
|
| 164 |
*/
|
| 165 |
public function verify_credentials() {
|
| 166 |
$values = $this->call('account/verify_credentials', array(), 'GET', TRUE);
|
| 167 |
if (!$values) {
|
| 168 |
return FALSE;
|
| 169 |
}
|
| 170 |
return new TwitterUser($values);
|
| 171 |
}
|
| 172 |
|
| 173 |
|
| 174 |
/**
|
| 175 |
* Method for calling any twitter api resource
|
| 176 |
*/
|
| 177 |
public function call($path, $params = array(), $method = 'GET', $use_auth = FALSE) {
|
| 178 |
$url = $this->create_url($path);
|
| 179 |
|
| 180 |
try {
|
| 181 |
if ($use_auth) {
|
| 182 |
$response = $this->auth_request($url, $params, $method);
|
| 183 |
}
|
| 184 |
else {
|
| 185 |
$response = $this->request($url, $params, $method);
|
| 186 |
}
|
| 187 |
}
|
| 188 |
catch (TwitterException $e) {
|
| 189 |
return FALSE;
|
| 190 |
}
|
| 191 |
|
| 192 |
if (!$response) {
|
| 193 |
return FALSE;
|
| 194 |
}
|
| 195 |
|
| 196 |
return $this->parse_response($response);
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Perform an authentication required request.
|
| 201 |
*/
|
| 202 |
protected function auth_request($path, $params = array(), $method = 'GET') {
|
| 203 |
if (empty($this->username) || empty($this->password)) {
|
| 204 |
return false;
|
| 205 |
}
|
| 206 |
|
| 207 |
return $this->request($path, $params, $method, TRUE);
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Perform a request
|
| 212 |
*/
|
| 213 |
protected function request($url, $params = array(), $method = 'GET', $use_auth = FALSE) {
|
| 214 |
$data = '';
|
| 215 |
if (count($params) > 0) {
|
| 216 |
if ($method == 'GET') {
|
| 217 |
$url .= '?'. http_build_query($params, '', '&');
|
| 218 |
}
|
| 219 |
else {
|
| 220 |
$data = http_build_query($params, '', '&');
|
| 221 |
}
|
| 222 |
}
|
| 223 |
|
| 224 |
$headers = array();
|
| 225 |
|
| 226 |
if ($use_auth) {
|
| 227 |
$headers['Authorization'] = 'Basic '. base64_encode($this->username .':'. $this->password);
|
| 228 |
$headers['Content-type'] = 'application/x-www-form-urlencoded';
|
| 229 |
}
|
| 230 |
|
| 231 |
$response = drupal_http_request($url, $headers, $method, $data);
|
| 232 |
if (!$response->error) {
|
| 233 |
return $response->data;
|
| 234 |
}
|
| 235 |
else {
|
| 236 |
$error = $response->error;
|
| 237 |
$data = $this->parse_response($response->data);
|
| 238 |
if ($data['error']) {
|
| 239 |
$error = $data['error'];
|
| 240 |
}
|
| 241 |
throw new TwitterException($error);
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
protected function parse_response($response, $format = NULL) {
|
| 246 |
if (empty($format)) {
|
| 247 |
$format = $this->format;
|
| 248 |
}
|
| 249 |
|
| 250 |
switch ($format) {
|
| 251 |
case 'json':
|
| 252 |
return json_decode($response, TRUE);
|
| 253 |
}
|
| 254 |
}
|
| 255 |
|
| 256 |
protected function create_url($path, $format = NULL) {
|
| 257 |
if (is_null($format)) {
|
| 258 |
$format = $this->format;
|
| 259 |
}
|
| 260 |
|
| 261 |
$url = 'http://'. $this->host .'/'. $path;
|
| 262 |
if (!empty($format)) {
|
| 263 |
$url .= '.'. $this->format;
|
| 264 |
}
|
| 265 |
return $url;
|
| 266 |
}
|
| 267 |
}
|
| 268 |
|
| 269 |
/**
|
| 270 |
* A class to provide OAuth enabled access to the twitter API
|
| 271 |
*/
|
| 272 |
class TwitterOAuth extends Twitter {
|
| 273 |
|
| 274 |
protected $signature_method;
|
| 275 |
|
| 276 |
protected $consumer;
|
| 277 |
|
| 278 |
protected $token;
|
| 279 |
|
| 280 |
public function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
|
| 281 |
$this->signature_method = new OAuthSignatureMethod_HMAC_SHA1();
|
| 282 |
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
|
| 283 |
if (!empty($oauth_token) && !empty($oauth_token_secret)) {
|
| 284 |
$this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
|
| 285 |
}
|
| 286 |
}
|
| 287 |
|
| 288 |
public function get_request_token() {
|
| 289 |
$url = $this->create_url('oauth/request_token', '');
|
| 290 |
try {
|
| 291 |
$response = $this->auth_request($url);
|
| 292 |
}
|
| 293 |
catch (TwitterException $e) {
|
| 294 |
}
|
| 295 |
parse_str($response, $token);
|
| 296 |
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
|
| 297 |
return $token;
|
| 298 |
}
|
| 299 |
|
| 300 |
public function get_authorize_url($token) {
|
| 301 |
$url = $this->create_url('oauth/authorize', '');
|
| 302 |
$url.= '?oauth_token=' . $token['oauth_token'];
|
| 303 |
|
| 304 |
return $url;
|
| 305 |
}
|
| 306 |
|
| 307 |
public function get_authenticate_url($token) {
|
| 308 |
$url = $this->create_url('oauth/authenticate', '');
|
| 309 |
$url.= '?oauth_token=' . $token['oauth_token'];
|
| 310 |
|
| 311 |
return $url;
|
| 312 |
}
|
| 313 |
|
| 314 |
public function get_access_token() {
|
| 315 |
$url = $this->create_url('oauth/access_token', '');
|
| 316 |
try {
|
| 317 |
$response = $this->auth_request($url);
|
| 318 |
}
|
| 319 |
catch (TwitterException $e) {
|
| 320 |
}
|
| 321 |
parse_str($response, $token);
|
| 322 |
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
|
| 323 |
return $token;
|
| 324 |
}
|
| 325 |
|
| 326 |
public function auth_request($url, $params = array(), $method = 'GET') {
|
| 327 |
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $params);
|
| 328 |
$request->sign_request($this->signature_method, $this->consumer, $this->token);
|
| 329 |
switch ($method) {
|
| 330 |
case 'GET':
|
| 331 |
return $this->request($request->to_url());
|
| 332 |
case 'POST':
|
| 333 |
return $this->request($request->get_normalized_http_url(), $request->to_postdata());
|
| 334 |
}
|
| 335 |
}
|
| 336 |
|
| 337 |
}
|
| 338 |
|
| 339 |
class TwitterSearch extends Twitter {
|
| 340 |
|
| 341 |
protected $host = 'search.twitter.com';
|
| 342 |
|
| 343 |
public function search($params = array()) {
|
| 344 |
|
| 345 |
}
|
| 346 |
}
|
| 347 |
|
| 348 |
/**
|
| 349 |
* Class for containing an individual twitter status.
|
| 350 |
*/
|
| 351 |
class TwitterStatus {
|
| 352 |
/**
|
| 353 |
* @var created_at
|
| 354 |
*/
|
| 355 |
public $created_at;
|
| 356 |
|
| 357 |
public $id;
|
| 358 |
|
| 359 |
public $text;
|
| 360 |
|
| 361 |
public $source;
|
| 362 |
|
| 363 |
public $truncated;
|
| 364 |
|
| 365 |
public $favorited;
|
| 366 |
|
| 367 |
public $in_reply_to_status_id;
|
| 368 |
|
| 369 |
public $in_reply_to_user_id;
|
| 370 |
|
| 371 |
public $in_reply_to_screen_name;
|
| 372 |
|
| 373 |
public $user;
|
| 374 |
|
| 375 |
/**
|
| 376 |
* Constructor for TwitterStatus
|
| 377 |
*/
|
| 378 |
public function __construct($values = array()) {
|
| 379 |
$this->created_at = $values['created_at'];
|
| 380 |
$this->id = $values['id'];
|
| 381 |
$this->text = $values['text'];
|
| 382 |
$this->source = $values['source'];
|
| 383 |
$this->truncated = $values['truncated'];
|
| 384 |
$this->favorited = $values['favorited'];
|
| 385 |
$this->in_reply_to_status_id = $values['in_reply_to_status_id'];
|
| 386 |
$this->in_reply_to_user_id = $values['in_reply_to_user_id'];
|
| 387 |
$this->in_reply_to_screen_name = $values['in_reply_to_screen_name'];
|
| 388 |
if (isset($values['user'])) {
|
| 389 |
$this->user = new TwitterUser($values['user']);
|
| 390 |
}
|
| 391 |
}
|
| 392 |
|
| 393 |
}
|
| 394 |
|
| 395 |
class TwitterUser {
|
| 396 |
|
| 397 |
public $id;
|
| 398 |
|
| 399 |
public $screen_name;
|
| 400 |
|
| 401 |
public $name;
|
| 402 |
|
| 403 |
public $location;
|
| 404 |
|
| 405 |
public $description;
|
| 406 |
|
| 407 |
public $profile_image_url;
|
| 408 |
|
| 409 |
public $url;
|
| 410 |
|
| 411 |
public $followers_count;
|
| 412 |
|
| 413 |
public $protected;
|
| 414 |
|
| 415 |
public $status;
|
| 416 |
|
| 417 |
protected $password;
|
| 418 |
|
| 419 |
protected $oauth_token;
|
| 420 |
|
| 421 |
protected $oauth_token_secret;
|
| 422 |
|
| 423 |
public function __construct($values = array()) {
|
| 424 |
$this->id = $values['id'];
|
| 425 |
$this->screen_name = $values['screen_name'];
|
| 426 |
$this->name = $values['name'];
|
| 427 |
$this->location = $values['location'];
|
| 428 |
$this->description = $values['description'];
|
| 429 |
$this->profile_image_url = $values['profile_image_url'];
|
| 430 |
$this->url = $values['url'];
|
| 431 |
$this->followers_count = $values['followers_count'];
|
| 432 |
$this->protected = $values['protected'];
|
| 433 |
if ($values['status']) {
|
| 434 |
$this->status = new TwitterStatus($values['status']);
|
| 435 |
}
|
| 436 |
}
|
| 437 |
|
| 438 |
public function get_auth() {
|
| 439 |
return array('password' => $this->password, 'oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret);
|
| 440 |
}
|
| 441 |
|
| 442 |
public function set_auth($values) {
|
| 443 |
$this->password = $values['password'];
|
| 444 |
$this->oauth_token = $values['oauth_token'];
|
| 445 |
$this->oauth_token_secret = $values['oauth_token_secret'];
|
| 446 |
}
|
| 447 |
}
|