3 * Abraham Williams (abraham@abrah.am) http://abrah.am
5 * Basic lib to work with Twitter's OAuth beta. This is untested and should not
6 * be used in production code. Twitter's beta could change at anytime.
9 * Fire Eagle code - http://github.com/myelin/fireeagle-php-lib
10 * twitterlibphp - http://github.com/jdp/twitterlibphp
13 //require_once('config.php');
14 //require_once('oauth_lib.php');
20 /* Contains the last HTTP status code returned */
22 /* Contains the last API call */
23 public $last_api_call;
24 /* Set up the API root URL */
25 //public $host = "https://api.twitter.com/1/";
26 public $host = API_URL;
27 /* Set timeout default */
29 /* Set connect timeout */
30 public $connecttimeout = 5;
32 public $ssl_verifypeer = FALSE;
34 public $type = 'json';
35 /* Decode returne json data */
36 public $decode_json = TRUE;
37 /* Immediately retry the API call if the response was not successful. */
38 //public $retry = TRUE;
39 public $source = 'rabr';
49 function accessTokenURL() { return 'https://twitter.com/oauth/access_token'; }
50 function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
51 function authorizeURL() { return 'https://twitter.com/oauth/authorize'; }
52 function requestTokenURL() { return 'https://twitter.com/oauth/request_token'; }
57 function lastStatusCode() { return $this->http_status; }
58 function lastAPICall() { return $this->last_api_call; }
61 * construct TwitterOAuth object
63 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
64 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
65 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
66 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
67 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
68 $this->screen_name = $_SESSION['access_token']['screen_name'];
69 $this->username = $_SESSION['access_token']['screen_name'];
70 $this->user_id = $_SESSION['access_token']['user_id'];
78 * Get a request_token from Twitter
80 * @returns a key/value array containing oauth_token and oauth_token_secret
82 function getRequestToken($oauth_callback = NULL) {
83 $parameters = array();
84 if (!empty($oauth_callback)) {
85 $parameters['oauth_callback'] = $oauth_callback;
87 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
88 $token = OAuthUtil::parse_parameters($request);
89 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
94 * Get the authorize URL
98 function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
99 if (is_array($token)) {
100 $token = $token['oauth_token'];
102 if (empty($sign_in_with_twitter)) {
103 return $this->authorizeURL() . "?oauth_token={$token}";
105 return $this->authenticateURL() . "?oauth_token={$token}";
110 * Exchange the request token and secret for an access token and
111 * secret, to sign API calls.
113 * @returns array("oauth_token" => the access token,
114 * "oauth_token_secret" => the access secret)
116 function getAccessToken($oauth_verifier = FALSE) {
117 $parameters = array();
118 if (!empty($oauth_verifier)) {
119 $parameters['oauth_verifier'] = $oauth_verifier;
121 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
122 $token = OAuthUtil::parse_parameters($request);
123 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
128 * GET wrappwer for oAuthRequest.
130 function get($url, $parameters = array()) {
131 $response = $this->oAuthRequest($url, 'GET', $parameters);
132 if($response == false){
135 if ($this->type == 'json' && $this->decode_json) {
136 return @json_decode($response);
137 }elseif($this->type == 'xml' && function_exists('simplexml_load_string')){
138 return @simplexml_load_string($response);
144 * POST wreapper for oAuthRequest.
146 function post($url, $parameters = array()) {
147 $response = $this->oAuthRequest($url, 'POST', $parameters);
148 if($response === false){
151 if ($this->type === 'json' && $this->decode_json) {
152 return json_decode($response);
153 }elseif($this->type == 'xml' && function_exists('simplexml_load_string')){
154 return simplexml_load_string($response);
160 * DELTE wrapper for oAuthReqeust.
162 function delete($url, $parameters = array()) {
163 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
164 if($response === false){
167 if ($this->type === 'json' && $this->decode_json) {
168 return json_decode($response);
169 }elseif($this->type == 'xml' && function_exists('simplexml_load_string')){
170 return simplexml_load_string($response);
176 * Format and sign an OAuth / API request
178 function oAuthRequest($url, $method, $parameters) {
179 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
180 $url = "{$this->host}{$url}.{$this->type}";
182 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
183 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
186 return $this->http($request->to_url(), 'GET');
188 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
193 * Make an HTTP request
195 * @return API results
197 function http($url, $method, $postfields = NULL) {
200 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
201 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
202 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
203 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
204 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
208 curl_setopt($ci, CURLOPT_POST, TRUE);
209 if (!empty($postfields)) {
210 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
214 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
215 if (!empty($postfields)) {
216 $url = "{$url}?{$postfields}";
220 curl_setopt($ci, CURLOPT_URL, $url);
221 $response = curl_exec($ci);
222 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
223 $this->last_api_call = $url;
228 /* ---------- API METHODS ---------- */
230 /* ---------- Block ---------- */
231 function blockingIDs(){
232 $url = 'blocks/blocking/ids';
233 return $this->get($url);
236 function blockingList($page){
237 $url = '/blocks/blocking';
240 $args['page'] = $page;
242 return $this->get($url, $args);
245 function blockUser($id){
246 $url = $this->host.'/blocks/create';
247 $url .= "/$id.$this->type";
248 return $this->post($url);
251 function isBlocked($id){
252 $url = $this->host.'/blocks/exists';
253 $url .= "/$id.$this->type";
254 return $this->get($url);
257 function unblockUser($id){
258 $url = $this->host.'/blocks/destroy';
259 $url .= "/$id.$this->type";
260 return $this->delete($url);
263 /* ---------- Messages ---------- */
264 function deleteDirectMessage($id){
265 $url = $this->host.'/direct_messages/destroy';
266 $url .= "/$id.$this->type";
267 return $this->delete($url);
270 function directMessages($page = false, $since_id = false, $count = null){
271 $url = '/direct_messages';
274 $args['since_id'] = $since_id;
276 $args['page'] = $page;
277 return $this->get($url, $args);
280 function sendDirectMessage($user, $text){
281 $url = '/direct_messages/new';
283 $args['user'] = $user;
285 $args['text'] = $text;
286 return $this->post($url, $args);
289 function sentDirectMessage($page = false, $since = false, $since_id = false){
290 $url = '/direct_messages/sent';
293 $args['since'] = $since;
295 $args['since_id'] = $since_id;
297 $args['page'] = $page;
298 return $this->get($url, $args);
301 /* ---------- List ---------- */
302 function addListMember($listid, $memberid){
303 $url = "/1/$this->username/$listid/members";
306 $args['id'] = $memberid;
308 return $this->post($url, $args);
311 function beAddedLists($username = '', $cursor = false){
312 $url = $this->host."/$username/lists/memberships.$this->type";
315 $args['cursor'] = $cursor;
317 return $this->get($url, $args);
320 function createList($name, $description, $isPortect){
321 $url = "/$this->username/lists";
324 $args['name'] = $name;
327 $args['description'] = $description;
330 $args['mode'] = 'private';
332 return $this->post($url, $args);
335 function createdLists($username = '', $cursor = false){
336 $url = $this->host."/$username/lists.$this->type";
339 $args['cursor'] = $cursor;
341 return $this->get($url, $args);
344 function deleteList($id){
345 $arr = explode('/', $id);
346 $url = "/$arr[0]/lists/$arr[1]";
347 return $this->delete($url);
350 function deleteListMember($id, $memberid){
351 $arr = explode("/", $id);
352 $url = "/1/".$arr[0]."/".$arr[1]."/members";
355 $args['id'] = $memberid;
357 return $this->delete($url, $args);
360 function editList($prename, $name, $description, $isProtect){
361 $url = "$this->username/lists/$prename";
364 $args['name'] = $name;
367 $args['description'] = $description;
370 $args['mode'] = "private";
372 return $this->post($url, $args);
375 function followedLists($username = '', $cursor = false){
376 $url = $this->host."/$username/lists/subscriptions.$this->type";
379 $args['cursor'] = $cursor;
381 return $this->get($url, $args);
384 function followList($id){
385 $arr = explode("/", $id);
386 $url = $this->host."/1/$arr[0]/$arr[1]/subscribers/$this->type";
387 return $this->post($url, $args);
390 function isFollowedList($id){
391 $arr = explode('/', $id);
392 $url = $this->host."/$arr[0]/$arr[1]/subscribers/$this->username.$this->type";
393 return $this->get($url);
396 function listFollowers($id, $cursor = false){
397 $arr = explode('/', $id);
398 $url = $this->host."/1/$arr[0]/$arr[1]/subscribers.$this->type";
401 $args['cursor'] = $cursor;
403 return $this->get($url, $args);
406 function listInfo($id){
407 $arr = explode('/', $id);
408 $url = $this->host."/$arr[0]/lists/$arr[1].$this->type";
409 return $this->get($url);
412 function listMembers($id, $cursor = false){
413 $arr = explode("/", $id);
414 $url = $this->host."/1/$arr[0]/$arr[1]/members.$this->type";
417 $args['cursor'] = $cursor;
419 return $this->get($url, $args);
423 function listStatus($id, $page = false, $since_id = false){
424 $arr = explode('/', $id);
425 $url = $this->host."/$arr[0]/lists/$arr[1]/statuses.$this->type";
428 $args['page'] = $page;
431 $args['since_id'] = $since_id;
433 return $this->get($url, $args);
436 function unfollowList($id){
437 $arr = explode("/", $id);
438 $url = $this->host."/1/$arr[0]/$arr[1]/subscribers.$this->type";
439 return $this->delete($url);
442 /* ---------- Friendship ---------- */
443 function destroyUser($id){
444 $url = $this->host.'/friendships/destroy';
445 $url .= "/$id.$this->type";
446 return $this->delete($url);
449 function followers($id = false, $page = false, $count = 30){
450 $url = $this->host.'/statuses/followers';
451 $url .= $id ? "/$id.$this->type" : ".$this->type";
455 $args['count'] = (int) $count;
456 $args['cursor'] = $page ? $page : -1;
457 return $this->get($url, $args);
460 function followUser($id, $notifications = false){
461 $url = $this->host.'/friendships/create';
462 $url .= "/$id.$this->type";
465 $args['follow'] = true;
466 return $this->post($url, $args);
469 function friends($id = false, $page = false, $count = 30){
470 $url = $this->host.'/statuses/friends';
471 $url .= $id ? "/$id.$this->type" : ".$this->type";
476 $args['count'] = (int) $count;
477 $args['cursor'] = $page ? $page : -1;
478 return $this->get($url, $args);
481 function isFriend($user_a, $user_b){
482 $url = '/friendships/exists';
484 $args['user_a'] = $user_a;
485 $args['user_b'] = $user_b;
486 return $this->get($url, $args);
489 function relationship($target, $source = false){
490 $url = API_TWITTER.'/1/friendships/show.'.$this->type;
492 $args['target_screen_name'] = $target;
494 $args['source_screen_name'] = $source;
496 return $this->get($url, $args);
499 function showUser($id = false, $email = false, $user_id = false, $screen_name = false){
500 $url = '/users/show';
505 $args['id'] = $screen_name;
507 $args['id'] = $this->user_id;
509 return $this->get($url, $args);
512 /* ---------- Ratelimit ---------- */
513 function ratelimit(){
514 $url = '/account/rate_limit_status';
515 return $this->get($url);
518 function ratelimit_status(){
519 return $this->ratelimit();
522 /* ---------- Retweet ---------- */
523 function getRetweeters($id, $count = false){
524 $url = $this->host."/1/statuses/retweets/$id.$this->type";
526 $url .= "?count=$count";
528 return $this->get($url);
531 function retweet($id){
532 $url = "http://api.twitter.com/1/statuses/retweet/$id.$this->type";
533 return $this->post($url);
536 function retweets($id, $count = 20){
540 $url = "http://api.twitter.com/1/statuses/retweets/id.$this->type?count=$count";
541 return $this->get($url);
544 // Returns the 20 most recent retweets posted by the authenticating user.
545 function retweeted_by_me($page = false, $count = 20, $since_id = false, $max_id = false){
546 $url = "http://api.twitter.com/1/statuses/retweeted_by_me.$this->type";
549 $args['since_id'] = $since_id;
552 $args['max_id'] = $max_id;
555 $args['count'] = $count;
558 $args['page'] = $page;
560 return $this->get($url, $args);
563 // Returns the 20 most recent retweets posted by the authenticating user's friends.
564 function retweeted_to_me($page = false, $count = false, $since_id = false, $max_id = false){
565 $url = "http://api.twitter.com/1/statuses/retweeted_to_me.$this->type";
568 $args['since_id'] = $since_id;
571 $args['max_id'] = $max_id;
574 $args['count'] = $count;
577 $args['page'] = $page;
579 return $this->get($url, $args);
582 function retweets_of_me($page = false, $count = false, $since_id = false, $max_id = false){
583 $url = "http://api.twitter.com/1/statuses/retweets_of_me.$this->type";
586 $args['since_id'] = $since_id;
589 $args['max_id'] = $max_id;
592 $args['count'] = $count;
595 $args['page'] = $page;
597 return $this->get($url, $args);
600 /* ---------- Search ---------- */
601 function search($q = false, $page = false, $rpp = false){
607 $args['page'] = $page;
613 return $this->get($url, $args);
616 /* ---------- Spam ---------- */
617 function reportSpam($screen_name){
618 $url = API_TWITTER."/1/report_spam.$this->type";
620 $args['screen_name'] = $screen_name;
621 return $this->post($url, $args);
624 /* ---------- Timeline ---------- */
625 function deleteStatus($id){
626 $url = $this->host.'/statuses/destroy';
627 $url .= "/$id.$this->type";
628 return $this->delete($url);
631 function friendsTimeline($page = false, $since_id = false, $count = false){
632 $url = '/statuses/friends_timeline';
635 $args['page'] = $page;
637 $args['since_id'] = $since_id;
639 $args['count'] = $count;
640 return $this->get($url, $args);
643 function getFavorites($page = false){
647 $args['page'] = $page;
648 return $this->get($url, $args);
651 function makeFavorite($id){
652 $url = $this->host.'/favorites/create/';
653 $url .= "$id.$this->type";
654 return $this->post($url);
657 function publicTimeline($sinceid = false){
658 $url = '/statuses/public_timeline';
661 $args['since_id'] = $sinceid;
663 return $this->get($url, $args);
666 function removeFavorite($id){
667 $url = $this->host.'/favorites/destroy/';
668 $url .= "$id.$this->type";
669 return $this->post($url);
672 function replies($page = false, $since_id = false){
673 $url = '/statuses/mentions';
676 $args['page'] = (int) $page;
678 $args['since_id'] = $since_id;
679 return $this->get($url, $args);
682 function showStatus($id){
683 $url = $this->host.'/statuses/show';
684 $url .= "/$id.$this->type";
685 return $this->get($url);
688 function update($status, $replying_to = false){
690 $url = '/statuses/update';
692 $args['status'] = $status;
694 $args['in_reply_to_status_id'] = $replying_to;
695 return $this->post($url, $args);
696 }catch(Exception $ex){
697 echo $ex->getLine." : ".$ex->getMessage();
701 function userTimeline($page = false, $id = false, $count = false, $since_id = false){
702 $url = '/statuses/user_timeline';
705 $args['page'] = $page;
709 $args['count'] = $count;
711 $args['since_id'] = $since_id;
712 $response = $this->get($url, $args);
713 if($response->error === 'Not authorized'){
720 $url = "http://search.twitter.com/trends.$this->type";
721 return $this->get($url);
724 /* ---------- Misc. ---------- */
725 function twitterAvailable(){
727 if($this->get($url) == 'ok'){
733 function updateProfile($fields = array()){
734 $url = '/account/update_profile';
736 foreach( $fields as $pk => $pv ){
739 $args[$pk] = (string) substr( $pv, 0, 20 );
742 if( preg_match( '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $pv ) )
743 $args[$pk] = (string) $pv;
746 $args[$pk] = (string) substr( $pv, 0, 100 );
749 $args[$pk] = (string) substr( $pv, 0, 30 );
752 $args[$pk] = (string) substr( $pv, 0, 160 );
758 return $this->post($url, $args);
762 $url = $this->host.'/account/verify_credentials.'.$this->type;
763 return $this->get($url);
766 /* ---------- image upload ---------- */
767 function imglyUpload($image){
768 $imgly = new image_uploader();
769 return is_null($imgly->imgly($image));
772 /* ---------- twitese method ---------- */
773 function rank($page = false, $count = false){
774 $url = TWITESE_API_URL."/rank.$this->type";
777 $args['page'] = $page;
780 $args['count'] = $count;
782 return $this->get($url, $args);
785 function browse($page = false, $count = false){
786 $url = TWITESE_API_URL."/browse.$this->type";
789 $args['page'] = $page;
792 $args['count'] = $count;
794 return $this->get($url, $args);