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 = API_URL;
26 /* Set timeout default */
28 /* Set connect timeout */
29 public $connecttimeout = 30;
31 public $ssl_verifypeer = FALSE;
33 public $source = 'embr';
47 function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
48 function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; }
49 function authorizeURL() { return 'https://api.twitter.com/oauth/authorize'; }
50 function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
55 function lastStatusCode() { return $this->http_status; }
56 function lastAPICall() { return $this->last_api_call; }
59 * construct TwitterOAuth object
61 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
62 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
63 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
64 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
65 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
66 $this->screen_name = $_SESSION['access_token']['screen_name'];
67 $this->username = $_SESSION['access_token']['screen_name'];
68 $this->user_id = $_SESSION['access_token']['user_id'];
76 * Get a request_token from Twitter
78 * @returns a key/value array containing oauth_token and oauth_token_secret
80 function getRequestToken($oauth_callback = NULL) {
81 $parameters = array();
82 if (!empty($oauth_callback)) {
83 $parameters['oauth_callback'] = $oauth_callback;
85 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
86 $token = OAuthUtil::parse_parameters($request);
87 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
92 * Get the authorize URL
96 function getAuthorizeURL($token) {
97 if (is_array($token)) {
98 $token = $token['oauth_token'];
100 return $this->authorizeURL() . "?oauth_token={$token}";
104 * Exchange the request token and secret for an access token and
105 * secret, to sign API calls.
107 * @returns array("oauth_token" => the access token,
108 * "oauth_token_secret" => the access secret)
110 function getAccessToken($oauth_verifier = FALSE) {
111 $parameters = array();
112 if (!empty($oauth_verifier)) {
113 $parameters['oauth_verifier'] = $oauth_verifier;
115 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
116 $token = OAuthUtil::parse_parameters($request);
117 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
122 * GET wrappwer for oAuthRequest.
124 function get($url, $parameters = array()) {
125 $response = $this->oAuthRequest($url, 'GET', $parameters);
126 if($response == false){
129 return json_decode($response);
133 * POST wreapper for oAuthRequest.
135 function post($url, $parameters = array(), $multipart = NULL) {
136 $response = $this->oAuthRequest($url, 'POST', $parameters, $multipart);
137 if($response === false){
140 return json_decode($response);
144 * Format and sign an OAuth / API request, then make an HTTP request
146 function oAuthRequest($url, $method, $parameters, $multipart=NULL) {
147 if ($url[0] == '/') { //non-twitter.com api shall offer the entire url.
148 $url = "{$this->host}{$url}.json";
150 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
151 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
152 $request->set_http_header($multipart);
156 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
157 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
158 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
159 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
163 curl_setopt($ci, CURLOPT_URL, $request->to_url());
164 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
167 $postfields = $multipart ? $multipart : $request->to_postdata();
168 curl_setopt($ci, CURLOPT_URL, $request->get_normalized_http_url());
169 curl_setopt($ci, CURLOPT_HTTPHEADER, $request->http_header);
170 curl_setopt($ci, CURLOPT_POST, TRUE);
171 if (!empty($postfields)) {
172 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
176 $response = curl_exec($ci);
177 $this->http_header = $request->http_header;
178 $this->curl_info = curl_getinfo($ci);
179 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
180 $this->last_api_call = curl_getinfo($ci, CURLINFO_EFFECTIVE_URL);
188 /* ---------- API METHODS ---------- */
190 /* ---------- Block ---------- */
191 function blockingList($id, $cursor=-1, $skip_status = true){
192 $url = '/blocks/list';
195 $args['screen_name'] = $id;
197 $args['cursor'] = $cursor;
198 $args['skip_status'] = $skip_status;
199 return $this->get($url, $args);
202 function blockUser($id){
203 $url = "/blocks/create";
205 $args['screen_name'] = $id;
206 return $this->post($url, $args);
209 function unblockUser($id){
210 $url = "/blocks/destroy";
212 $args['screen_name'] = $id;
213 return $this->post($url, $args);
216 /* ---------- Messages ---------- */
217 function deleteDirectMessage($id){
218 $url = "/direct_messages/destroy";
221 return $this->post($url, $args);
224 function directMessages($since_id = false, $max_id = false, $count = false, $include_entities = true){
225 $url = '/direct_messages';
228 $args['since_id'] = $since_id;
230 $args['max_id'] = $max_id;
232 $args['count'] = $count;
233 if($include_entities)
234 $args['include_entities'] = $include_entities;
235 return $this->get($url, $args);
238 function sentDirectMessages($since_id = false, $max_id = false, $count = false, $include_entities = true){
239 $url = '/direct_messages/sent';
242 $args['since_id'] = $since_id;
244 $args['max_id'] = $max_id;
246 $args['count'] = $count;
247 if($include_entities)
248 $args['include_entities'] = $include_entities;
249 return $this->get($url, $args);
252 /* ---------- List ---------- */
253 function addListMember($listid, $id, $memberid){
254 $url = "/lists/members/create_all";
257 $args['slug'] = $listid;
259 $args['owner_screen_name'] = $id;
261 $args['screen_name'] = $memberid;
263 return $this->post($url, $args);
266 function beAddedLists($username = '', $cursor = false){
267 $url = "/lists/memberships";
270 $args['screen_name'] = $username;
272 $args['cursor'] = $cursor;
274 return $this->get($url, $args);
277 function createList($name, $description, $isPortect){
278 $url = "/lists/create";
281 $args['name'] = $name;
283 $args['description'] = $description;
285 $args['mode'] = 'private';
287 return $this->post($url, $args);
290 function myLists($username = false, $user_id = false, $count = false){
291 $url = "/lists/ownerships";
294 $args['screen_name'] = $username;
296 $args['screen_name'] = $this->username;
299 return $this->get($url, $args);
302 function deleteList($slug){
303 $url = "/lists/destroy";
305 $args['slug'] = $slug;
306 $args['owner_screen_name'] = $this->username;
307 return $this->post($url, $args);
310 function deleteListMember($slug, $owner, $memberid){
311 $url = "/lists/members/destroy_all";
313 $args['slug'] = $slug;
314 $args['owner_screen_name'] = $owner;
315 $args['user_id'] = $memberid;
317 return $this->post($url, $args);
320 function editList($prename, $name, $description, $isProtect){
321 $url = "/lists/update";
324 $args['slug'] = $prename;
326 $args['name'] = $name;
328 $args['description'] = $description;
330 $args['mode'] = "private";
331 return $this->post($url, $args);
334 function followedLists($username = '', $cursor = false){
335 $url = "/lists/subscriptions";
338 $args['screen_name'] = $username;
340 $args['cursor'] = $cursor;
341 return $this->get($url, $args);
344 function followList($id){
345 $url = "/lists/subscribers/create";
346 $arr = explode("/", $id);
348 $args['slug'] = $arr[1];
349 $args['owner_screen_name'] = $arr[0];
350 return $this->post($url, $args);
353 function isFollowedList($id){
354 $url = "/lists/subscribers/show";
355 $arr = explode('/', $id);
357 $args['owner_screen_name'] = $arr[0];
358 $args['slug'] = $arr[1];
359 $args['screen_name'] = $this->username;
360 return isset($this->get($url, $args)->screen_name);
363 function listFollowers($id, $cursor = false, $skip_status = true){
364 $url = "/lists/subscribers";
365 $arr = explode('/', $id);
367 $args['slug'] = $arr[1];
368 $args['owner_screen_name'] = $arr[0];
370 $args['cursor'] = $cursor;
372 $args['skip_status'] = $skip_status;
373 return $this->get($url, $args);
376 function listInfo($id){
377 $arr = explode('/', $id);
378 $url = "/lists/show";
380 $args['slug'] = $arr[1];
381 $args['owner_screen_name'] = $arr[0];
382 return $this->get($url, $args);
385 function listMembers($id, $cursor = false, $skip_status = true){
386 $url = "/lists/members";
387 $arr = explode("/", $id);
389 $args['slug'] = $arr[1];
390 $args['owner_screen_name'] = $arr[0];
392 $args['cursor'] = $cursor;
394 $args['skip_status'] = $skip_status;
395 return $this->get($url, $args);
399 function listStatus($id, $since_id = false, $max_id = false, $include_rts = true, $include_entities = true){
400 $arr = explode('/', $id);
401 $url = "/lists/statuses";
403 $args['slug'] = $arr[1];
404 $args['owner_screen_name'] = $arr[0];
406 $args['since_id'] = $since_id;
408 $args['max_id'] = $max_id;
410 $args['include_rts'] = $include_rts;
411 if($include_entities)
412 $args['include_entities'] = $include_entities;
413 return $this->get($url, $args);
416 function unfollowList($id){
417 $url = "/lists/subscribers/destroy";
418 $arr = explode("/", $id);
420 $args['slug'] = $arr[1];
421 $args['owner_screen_name'] = $arr[0];
422 return $this->post($url, $args);
425 /* ---------- Friendship ---------- */
426 function destroyUser($id){
427 $url = "/friendships/destroy";
429 $args['screen_name'] = $id;
430 return $this->post($url, $args);
433 function followers($id = false, $cursor = -1, $skip_status = true){ // GET statuses/friends is removed, try GET followers/list instead
434 $url = '/followers/list';
437 $args['screen_name'] = $id;
439 $args['cursor'] = $cursor;
440 $args['skip_status'] = $skip_status;
441 return $this->get($url, $args);
444 function followUser($id, $notifications = false){
445 $url = "/friendships/create";
447 $args['screen_name'] = $id;
449 $args['follow'] = true;
450 return $this->post($url, $args);
453 function friends($id = false, $cursor = -1, $skip_status = true){ // GET statuses/friends is removed, try GET friends/list instead
454 $url = '/friends/list';
457 $args['screen_name'] = $id;
459 $args['cursor'] = $cursor;
460 $args['skip_status'] = $skip_status;
461 return $this->get($url, $args);
464 function relationship($target_screen_name, $source_screen_name = false){
465 $url = '/friendships/show';
467 $args['target_screen_name'] = $target_screen_name;
468 $args['source_screen_name'] = $source_screen_name ? $source_screen_name : $this->username;
469 return $this->get($url, $args);
472 function showUser($screen_name = false, $user_id = false, $include_entities = true){
473 $url = '/users/show';
476 $args['screen_name'] = $screen_name;
478 $args['user_id'] = $user_id ? $user_id : $this->user_id;
479 return $this->get($url, $args);
482 /* ---------- Ratelimit ---------- */
483 /* this API changed a lot due to the
484 * update of twitter's limit policy,
485 * get only status limits here by
487 function ratelimit($resources = "statuses"){
488 $url = '/application/rate_limit_status';
490 $args['resources'] = $resources;
491 return $this->get($url, $args);
494 /* ---------- Retweet ---------- */
495 function retweet($id){
496 $url = "/statuses/retweet/$id";
497 return $this->post($url);
500 function retweets($id, $count = 20){
504 $url = "/statuses/retweets/$id";
506 $args['count'] = $count;
507 return $this->get($url,$args);
510 function retweets_of_me($count = false, $since_id = false, $max_id = false, $include_entities = true){
511 $url = '/statuses/retweets_of_me';
514 $args['since_id'] = $since_id;
516 $args['max_id'] = $max_id;
518 $args['count'] = $count;
519 if($include_entities)
520 $args['include_entities'] = $include_entities;
521 return $this->get($url, $args);
524 /* ---------- Search ---------- */
525 function savedSearches(){
526 $url = '/saved_searches/list';
527 return $this->get($url);
530 function deleteSavedSearch($ssid){
531 $url = "/saved_searches/destroy/{$ssid}";
532 return $this->post($url);
535 function saveSearch($query){
536 $url = "/saved_searches/create";
538 $args['query'] = $query;
539 return $this->post($url, $args);
542 function search($q = false, $since_id = false, $max_id = false, $include_entities = true){
543 $url = '/search/tweets';
551 $args['since_id'] = $since_id;
554 $args['max_id'] = $max_id;
556 if($include_entities){
557 $args['include_entities'] = $include_entities;
559 return $this->get($url, $args);
562 /* ---------- Spam ---------- */
563 function reportSpam($screen_name){
564 $url = '/users/report_spam';
566 $args['screen_name'] = $screen_name;
567 return $this->post($url, $args);
570 /* ---------- Timeline ---------- */
571 function deleteStatus($id){
572 $url = "/statuses/destroy/$id";
573 return $this->post($url);
576 function homeTimeline($since_id = false, $max_id = false, $count = false, $include_entities = true) {
577 $url = '/statuses/home_timeline';
580 $args['max_id'] = $max_id;
582 $args['since_id'] = $since_id;
584 $args['count'] = $count;
587 if($include_entities)
588 $args['include_entities'] = $include_entities;
589 return $this->get($url, $args);
592 function getFavorites($userid = false, $sinceid = false, $maxid = false, $count = false, $include_entities = true){
593 $url = '/favorites/list';
596 $args['screen_name'] = $userid;
598 $args['since_id'] = $sinceid;
600 $args['max_id'] = $maxid;
602 $args['count'] = $count;
603 if($include_entities)
604 $args['include_entities'] = $include_entities;
605 return $this->get($url, $args);
608 function makeFavorite($id){
609 $url = "/favorites/create";
613 return $this->post($url, $args);
616 function removeFavorite($id){
617 $url = "/favorites/destroy";
621 return $this->post($url, $args);
624 function replies($since_id = false, $max_id = false, $count = false, $include_entities = true){
625 $url = '/statuses/mentions_timeline';
628 $args['max_id'] = $max_id;
630 $args['since_id'] = $since_id;
632 $args['count'] = $count;
633 if($include_entities)
634 $args['include_entities'] = $include_entities;
635 return $this->get($url, $args);
638 function showStatus($id,$include_entities = true){
639 $url = "/statuses/show";
643 if($include_entities)
644 $args['include_entities'] = $include_entities;
645 return $this->get($url,$args);
648 function update($status, $replying_to = false,$include_entities = true){
650 $url = '/statuses/update';
652 $args['status'] = $status;
654 $args['in_reply_to_status_id'] = $replying_to;
655 if($include_entities)
656 $args['include_entities'] = $include_entities;
657 return $this->post($url, $args);
658 }catch(Exception $ex){
659 echo $ex->getLine." : ".$ex->getMessage();
663 function userTimeline($id = false, $since_id = false, $max_id = false, $count = false, $include_rts = true, $include_entities = true){
664 $url = '/statuses/user_timeline';
667 $args['max_id'] = $max_id;
669 $args['screen_name'] = $id;
671 $args['count'] = $count;
673 $args['since_id'] = $since_id;
675 $args['include_rts'] = $include_rts;
676 if($include_entities)
677 $args['include_entities'] = $include_entities;
678 $response = $this->get($url, $args);
682 function trends_closest($lat = false, $long=false) {
683 $url = "/trends/closest";
688 $args['long'] = $long;
689 return $this->get($url, $args);
692 function trends_place($id = 1) {
693 $url = "/trends/place";
697 return $this->get($url, $args);
699 /* ---------- Misc. ---------- */
700 function veverify($skip_status = false){
701 $url = '/account/verify_credentials';
702 $args = array('skip_status' => $skip_status);
703 return $this->get($url,$args);
706 function updateProfile($fields = array(), $skip_status = true){
707 $url = '/account/update_profile';
709 foreach( $fields as $pk => $pv ){
712 $args[$pk] = (string) substr( $pv, 0, 20 );
715 $args[$pk] = (string) substr( $pv, 0, 100 );
718 $args[$pk] = (string) substr( $pv, 0, 30 );
721 $args[$pk] = (string) substr( $pv, 0, 160 );
726 $args['skip_status'] = $skip_status;
728 return $this->post($url, $args);
732 function updateProfileImage($image, $skip_status = true) {
733 $url = '/account/update_profile_image';
736 $args['image']=$image;
737 $args['skip_status']=$skip_status;
739 return $this->post($url, $args);
742 function updateProfileBackground($image, $tile = false, $skip_status = true) {
743 $url = '/account/update_profile_background_image';
746 $args['image']=$image;
749 $args['tile']=($tile === 'true');
751 $args['skip_status']=$skip_status;
752 return $this->post($url, $args);
755 function updateMedia($status,$image,$replying_to = false) {
756 $url = 'statuses/update_with_media';
758 if($status) $args['status'] = $status;
759 if($replying_to) $args['in_reply_to_status_id'] = $replying_to;
761 if($image) $mul['media'][] = $image;
762 return $this->post($url,$args,$mul);