| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright (c) <2008> Justin Poliey <jdp34@njit.edu> |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person |
| 6 |
* obtaining a copy of this software and associated documentation |
| 7 |
* files (the "Software"), to deal in the Software without |
| 8 |
* restriction, including without limitation the rights to use, |
| 9 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the |
| 11 |
* Software is furnished to do so, subject to the following |
| 12 |
* conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be |
| 15 |
* included in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 19 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/** |
| 28 |
* Twitterlibphp is a PHP implementation of the Twitter API, allowing you |
| 29 |
* to take advantage of it from within your PHP applications. |
| 30 |
* |
| 31 |
* @author Justin Poliey <jdp34@njit.edu> |
| 32 |
* @package twitterlibphp |
| 33 |
*/ |
| 34 |
|
| 35 |
/** |
| 36 |
* Twitter API abstract class |
| 37 |
* @package twitterlibphp |
| 38 |
*/ |
| 39 |
abstract class TwitterBase { |
| 40 |
|
| 41 |
/** |
| 42 |
* the last HTTP status code returned |
| 43 |
* @access private |
| 44 |
* @var integer |
| 45 |
*/ |
| 46 |
private $http_status; |
| 47 |
|
| 48 |
/** |
| 49 |
* the whole URL of the last API call |
| 50 |
* @access private |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
private $last_api_call; |
| 54 |
|
| 55 |
/** |
| 56 |
* the application calling the API |
| 57 |
* @access private |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
private $application_source; |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the 20 most recent statuses from non-protected users who have set a custom user icon. |
| 64 |
* @param string $format Return format |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
function getPublicTimeline($format = 'xml') { |
| 68 |
return $this->apiCall('statuses/public_timeline', 'get', $format, array(), false); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns the 20 most recent statuses posted by the authenticating user and that user's friends. |
| 73 |
* @param array $options Options to pass to the method |
| 74 |
* @param string $format Return format |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
function getFriendsTimeline($options = array(), $format = 'xml') { |
| 78 |
return $this->apiCall('statuses/friends_timeline', 'get', $format, $options); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the 20 most recent statuses posted from the authenticating user. |
| 83 |
* @param array $options Options to pass to the method |
| 84 |
* @param string $format Return format |
| 85 |
* @return string |
| 86 |
*/ |
| 87 |
function getUserTimeline($options = array(), $format = 'xml') { |
| 88 |
return $this->apiCall('statuses/user_timeline', 'get', $format, $options, true); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns the 20 most recent mentions (status containing @username) for the authenticating user. |
| 93 |
* @param array $options Options to pass to the method |
| 94 |
* @param string $format Return format |
| 95 |
* @return string |
| 96 |
*/ |
| 97 |
function getMentions($options = array(), $format = 'xml') { |
| 98 |
return $this->apiCall("statuses/mentions", 'get', $format, $options); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user. |
| 103 |
* @param array $options Options to pass to the method |
| 104 |
* @param string $format Return format |
| 105 |
* @return string |
| 106 |
* @deprecated |
| 107 |
*/ |
| 108 |
function getReplies($options = array(), $format = 'xml') { |
| 109 |
return $this->apiCall('statuses/replies', 'get', $format, $options); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns a single status, specified by the $id parameter. |
| 114 |
* @param string|integer $id The numerical ID of the status to retrieve |
| 115 |
* @param string $format Return format |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
function getStatus($id, $format = 'xml') { |
| 119 |
return $this->apiCall("statuses/show/{$id}", 'get', $format, array(), false); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Updates the authenticated user's status. |
| 124 |
* @param string $status Text of the status, no URL encoding necessary |
| 125 |
* @param string|integer $reply_to ID of the status to reply to. Optional |
| 126 |
* @param string $format Return format |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
function updateStatus($status, $reply_to = null, $format = 'xml') { |
| 130 |
$options = array('status' => $status); |
| 131 |
if ($reply_to) { |
| 132 |
$options['in_reply_to_status_id'] = $reply_to; |
| 133 |
} |
| 134 |
return $this->apiCall('statuses/update', 'post', $format, $options); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Destroys the status specified by the required ID parameter. The authenticating user must be the author of the specified status. |
| 139 |
* @param integer|string $id ID of the status to destroy |
| 140 |
* @param string $format Return format |
| 141 |
* @return string |
| 142 |
*/ |
| 143 |
function destroyStatus($id, $format = 'xml') { |
| 144 |
return $this->apiCall("statuses/destroy/{$id}", 'post', $format, $options); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the authenticating user's friends, each with current status inline. |
| 149 |
* @param array $options Options to pass to the method |
| 150 |
* @param string $format Return format |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
function getFriends($options = array(), $format = 'xml') { |
| 154 |
return $this->apiCall('statuses/friends', 'get', $format, $options, false); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Returns the authenticating user's followers, each with current status inline. |
| 159 |
* @param array $options Options to pass to the method |
| 160 |
* @param string $format Return format |
| 161 |
* @return string |
| 162 |
*/ |
| 163 |
function getFollowers($options = array(), $format = 'xml') { |
| 164 |
return $this->apiCall('statuses/followers', 'get', $format, $options); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns extended information of a given user. |
| 169 |
* @param array $options Options to pass to the method |
| 170 |
* @param string $format Return format |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
function showUser($options = array(), $format = 'xml') { |
| 174 |
if (!array_key_exists('id', $options) && !array_key_exists('user_id', $options) && !array_key_exists('screen_name', $options)) { |
| 175 |
$options['id'] = substr($this->credentials, 0, strpos($this->credentials, ':')); |
| 176 |
} |
| 177 |
return $this->apiCall('users/show', 'get', $format, $options, false); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns a list of the 20 most recent direct messages sent to the authenticating user. |
| 182 |
* @param array $options Options to pass to the method |
| 183 |
* @param string $format Return format |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
function getMessages($options = array(), $format = 'xml') { |
| 187 |
return $this->apiCall('direct_messages', 'get', $format, $options); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns a list of the 20 most recent direct messages sent by the authenticating user. |
| 192 |
* @param array $options Options to pass to the method |
| 193 |
* @param string $format Return format |
| 194 |
* @return string |
| 195 |
*/ |
| 196 |
function getSentMessages($options = array(), $format = 'xml') { |
| 197 |
return $this->apiCall('direct_messages/sent', 'get', $format, $options); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Sends a new direct message to the specified user from the authenticating user. |
| 202 |
* @param string $user The ID or screen name of a recipient |
| 203 |
* @param string $text The message to send |
| 204 |
* @param string $format Return format |
| 205 |
* @return string |
| 206 |
*/ |
| 207 |
function newMessage($user, $text, $format = 'xml') { |
| 208 |
$options = array( |
| 209 |
'user' => $user, |
| 210 |
'text' => $text |
| 211 |
); |
| 212 |
return $this->apiCall('direct_messages/new', 'post', $format, $options); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Destroys the direct message specified in the required $id parameter. |
| 217 |
* @param integer|string $id The ID of the direct message to destroy |
| 218 |
* @param string $format Return format |
| 219 |
* @return string |
| 220 |
*/ |
| 221 |
function destroyMessage($id, $format = 'xml') { |
| 222 |
return $this->apiCall("direct_messages/destroy/{$id}", 'post', $format, $options); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Befriends the user specified in the ID parameter as the authenticating user. |
| 227 |
* @param array $options Options to pass to the method |
| 228 |
* @param string $format Return format |
| 229 |
* @return string |
| 230 |
*/ |
| 231 |
function createFriendship($options = array(), $format = 'xml') { |
| 232 |
if (!array_key_exists('follow', $options)) { |
| 233 |
$options['follow'] = 'true'; |
| 234 |
} |
| 235 |
return $this->apiCall('friendships/create', 'post', $format, $options); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Discontinues friendship with the user specified in the ID parameter as the authenticating user. |
| 240 |
* @param integer|string $id The ID or screen name of the user to unfriend |
| 241 |
* @param string $format Return format |
| 242 |
* @return string |
| 243 |
*/ |
| 244 |
function destroyFriendship($id, $format = 'xml') { |
| 245 |
$options = array('id' => $id); |
| 246 |
return $this->apiCall('friendships/destroy', 'post', $format, $options); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Tests if a friendship exists between two users. |
| 251 |
* @param integer|string $user_a The ID or screen name of the first user |
| 252 |
* @param integer|string $user_b The ID or screen name of the second user |
| 253 |
* @param string $format Return format |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
function friendshipExists($user_a, $user_b, $format = 'xml') { |
| 257 |
$options = array( |
| 258 |
'user_a' => $user_a, |
| 259 |
'user_b' => $user_b |
| 260 |
); |
| 261 |
return $this->apiCall('friendships/exists', 'get', $format, $options); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns an array of numeric IDs for every user the specified user is followed by. |
| 266 |
* @param array $options Options to pass to the method |
| 267 |
* @param string $format Return format |
| 268 |
* @return string |
| 269 |
*/ |
| 270 |
function getFriendIDs($options = array(), $format = 'xml') { |
| 271 |
return $this->apiCall('friends/ids', 'get', $format, $options); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns an array of numeric IDs for every user the specified user is following. |
| 276 |
* @param array $options Options to pass to the method |
| 277 |
* @param string $format Return format |
| 278 |
* @return string |
| 279 |
*/ |
| 280 |
function getFollowerIDs($options = array(), $format = 'xml') { |
| 281 |
return $this->apiCall('followers/ids', 'get', $format, $options); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; returns a 401 status code and an error message if not. |
| 286 |
* @param string $format Return format |
| 287 |
* @return string |
| 288 |
*/ |
| 289 |
function verifyCredentials($format = 'xml') { |
| 290 |
return $this->apiCall('account/verify_credentials', 'get', $format, array()); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Returns the remaining number of API requests available to the requesting user before the API limit is reached for the current hour. |
| 295 |
* @param boolean $authenticate Authenticate before calling method |
| 296 |
* @param string $format Return format |
| 297 |
* @return string |
| 298 |
*/ |
| 299 |
function rateLimitStatus($authenticate = false, $format = 'xml') { |
| 300 |
return $this->apiCall('account/rate_limit_status', 'get', $format, array(), $authenticate); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Ends the session of the authenticating user, returning a null cookie. |
| 305 |
* @param string $format Return format |
| 306 |
* @return string |
| 307 |
*/ |
| 308 |
function endSession($format = 'xml') { |
| 309 |
return $this->apiCall('account/end_session', 'post', $format, array()); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Sets which device Twitter delivers updates to for the authenticating user. |
| 314 |
* @param string $device The delivery device used. Must be sms, im, or none |
| 315 |
* @return string |
| 316 |
*/ |
| 317 |
function updateDeliveryDevice($device, $format = 'xml') { |
| 318 |
$options = array('device' => $device); |
| 319 |
return $this->apiCall('account/update_delivery_device', 'post', $format, $options); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Sets one or more hex values that control the color scheme of the authenticating user's profile page on twitter.com. |
| 324 |
* @param array $options Options to pass to the method |
| 325 |
* @param string $format Return format |
| 326 |
* @return string |
| 327 |
*/ |
| 328 |
function updateProfileColors($options, $format = 'xml') { |
| 329 |
return $this->apiCall('account/update_profile_colors', 'post', $format, $options); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Sets values that users are able to set under the "Account" tab of their settings page. |
| 334 |
* @param array $options Options to pass to the method |
| 335 |
* @param string $format Return format |
| 336 |
* @return string |
| 337 |
*/ |
| 338 |
function updateProfile($options, $format = 'xml') { |
| 339 |
return $this->apiCall('account/update_profile', 'post', $format, array()); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* Returns the 20 most recent favorite statuses for the authenticating user or user specified by the ID parameter in the requested format. |
| 345 |
* @param array $options Options to pass to the method |
| 346 |
* @param string $format Return format |
| 347 |
* @return string |
| 348 |
*/ |
| 349 |
function getFavorites($options = array(), $format = 'xml') { |
| 350 |
return $this->apiCall('favorites', 'get', $format, $options); |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Favorites the status specified in the ID parameter as the authenticating user. |
| 355 |
* @param integer|string $id The ID of the status to favorite |
| 356 |
* @param string $format Return format |
| 357 |
* @return string |
| 358 |
*/ |
| 359 |
function createFavorite($id, $format = 'xml') { |
| 360 |
return $this->apiCall("favorites/create/{$id}", 'post', $format, array()); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Un-favorites the status specified in the ID parameter as the authenticating user. |
| 365 |
* @param integer|string $id The ID of the status to un-favorite |
| 366 |
* @param string $format Return format |
| 367 |
* @return string |
| 368 |
*/ |
| 369 |
function destroyFavorite($id, $format = 'xml') { |
| 370 |
return $this->apiCall("favorites/destroy/{$id}", 'post', $format, array()); |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Enables notifications for updates from the specified user to the authenticating user. |
| 375 |
* @param integer|string $id The ID or screen name of the user to follow |
| 376 |
* @param string $format Return format |
| 377 |
* @return string |
| 378 |
*/ |
| 379 |
function follow($id, $format = 'xml') { |
| 380 |
$options = array('id' => $id); |
| 381 |
return $this->apiCall('notifications/follow', 'post', $format, $options); |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Disables notifications for updates from the specified user to the authenticating user. |
| 386 |
* @param integer|string $id The ID or screen name of the user to leave |
| 387 |
* @param string $format Return format |
| 388 |
* @return string |
| 389 |
*/ |
| 390 |
function leave($id, $format = 'xml') { |
| 391 |
$options = array('id' => $id); |
| 392 |
return $this->apiCall('notifications/leave', 'post', $format, $options); |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Blocks the user specified in the ID parameter as the authenticating user. |
| 397 |
* @param integer|string $id The ID or screen name of the user to block |
| 398 |
* @param string $format Return format |
| 399 |
* @return string |
| 400 |
*/ |
| 401 |
function createBlock($id, $format = 'xml') { |
| 402 |
$options = array('id' => $id); |
| 403 |
return $this->apiCall('blocks/create', 'post', $format, $options); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Unblocks the user specified in the ID parameter as the authenticating user. |
| 408 |
* @param integer|string $id The ID or screen name of the user to unblock |
| 409 |
* @param string $format Return format |
| 410 |
* @return string |
| 411 |
*/ |
| 412 |
function destroyBlock($id, $format = 'xml') { |
| 413 |
$options = array('id' => $id); |
| 414 |
return $this->apiCall('blocks/destroy', 'post', $format, $options); |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Returns if the authenticating user is blocking a target user. |
| 419 |
* @param array $options Options to pass to the method |
| 420 |
* @param string $format Return format |
| 421 |
* @return string |
| 422 |
*/ |
| 423 |
function blockExists($options, $format = 'xml') { |
| 424 |
return $this->apiCall('blocks/exists', 'get', $format, $options); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Returns an array of user objects that the authenticating user is blocking. |
| 429 |
* @param array $options Options to pass to the method |
| 430 |
* @param string $format Return format |
| 431 |
* @return string |
| 432 |
*/ |
| 433 |
function getBlocking($options, $format = 'xml') { |
| 434 |
return $this->apiCall('blocks/blocking', 'get', $format, $options); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Returns an array of numeric user ids the authenticating user is blocking. |
| 439 |
* @param array $options Options to pass to the method |
| 440 |
* @param string $format Return format |
| 441 |
* @return string |
| 442 |
*/ |
| 443 |
function getBlockingIDs($format = 'xml') { |
| 444 |
return $this->apiCall('blocks/blocking/ids', 'get', $format, array()); |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Returns the string "ok" in the requested format with a 200 OK HTTP status code. |
| 449 |
* @param string $format Return format |
| 450 |
* @return string |
| 451 |
*/ |
| 452 |
function test($format = 'xml') { |
| 453 |
return $this->apiCall('help/test', 'get', $format, array(), false); |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Returns the last HTTP status code |
| 458 |
* @return integer |
| 459 |
*/ |
| 460 |
function lastStatusCode() { |
| 461 |
return $this->http_status; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Returns the URL of the last API call |
| 466 |
* @return string |
| 467 |
*/ |
| 468 |
function lastApiCall() { |
| 469 |
return $this->last_api_call; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Access to the Twitter API through HTTP auth |
| 475 |
* @package twitterlibphp |
| 476 |
*/ |
| 477 |
class Twitter extends TwitterBase { |
| 478 |
|
| 479 |
/** |
| 480 |
* the Twitter credentials in HTTP format, username:password |
| 481 |
* @access private |
| 482 |
* @var string |
| 483 |
*/ |
| 484 |
var $credentials; |
| 485 |
|
| 486 |
/** |
| 487 |
* Fills in the credentials {@link $credentials} and the application source {@link $application_source}. |
| 488 |
* @param string $username Twitter username |
| 489 |
* @param string $password Twitter password |
| 490 |
* @param $source string Optional. Name of the application using the API |
| 491 |
*/ |
| 492 |
function __construct($username, $password, $source = null) { |
| 493 |
$this->credentials = sprintf("%s:%s", $username, $password); |
| 494 |
$this->application_source = $source; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Executes an API call |
| 499 |
* @param string $twitter_method The Twitter method to call |
| 500 |
* @param string $http_method The HTTP method to use |
| 501 |
* @param string $format Return format |
| 502 |
* @param array $options Options to pass to the Twitter method |
| 503 |
* @param boolean $require_credentials Whether or not credentials are required |
| 504 |
* @return string |
| 505 |
*/ |
| 506 |
protected function apiCall($twitter_method, $http_method, $format, $options, $require_credentials = true) { |
| 507 |
$curl_handle = curl_init(); |
| 508 |
$api_url = sprintf('http://twitter.com/%s.%s', $twitter_method, $format); |
| 509 |
if (($http_method == 'get') && (count($options) > 0)) { |
| 510 |
$api_url .= '?' . http_build_query($options); |
| 511 |
} |
| 512 |
//echo $api_url . "\n"; |
| 513 |
curl_setopt($curl_handle, CURLOPT_URL, $api_url); |
| 514 |
if ($require_credentials) { |
| 515 |
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials); |
| 516 |
} |
| 517 |
if ($http_method == 'post') { |
| 518 |
curl_setopt($curl_handle, CURLOPT_POST, true); |
| 519 |
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query($options)); |
| 520 |
} |
| 521 |
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE); |
| 522 |
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:')); |
| 523 |
$twitter_data = curl_exec($curl_handle); |
| 524 |
$this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); |
| 525 |
$this->last_api_call = $api_url; |
| 526 |
curl_close($curl_handle); |
| 527 |
return $twitter_data; |
| 528 |
} |
| 529 |
|
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* TODO: Add TwitterOAuth class |
| 534 |
*/ |
| 535 |
?> |
| 536 |
|