Overview

Namespaces

  • Sleepy
  • Module
    • Authentication
    • CSV
    • DB
    • FormBuilder
    • FSDB
    • IP2Country
    • Mailer
    • MobiDetect
    • Navigation
    • StaticCache
  • PHP

Classes

  • Sleepy\Debug
  • Sleepy\Hook
  • Sleepy\Router
  • Sleepy\SM
  • Sleepy\Template

Exceptions

  • Sleepy\RouteNotFound
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Module;
  4: require 'vendor/twitteroauth-0.5.3/autoload.php';
  5: use Abraham\TwitterOAuth\TwitterOAuth;
  6: 
  7: define(
  8:     'TWITTER_CONSUMER_KEY',
  9:     '857XlKo7iJSUo461EkuqpFE8s'
 10: );
 11: 
 12: define(
 13:     'TWITTER_CONSUMER_SECRET',
 14:     'tCkXdlBud8aczffTsce62wdtnNdpl56hkNXmmCPnDiCjIDOphT'
 15: );
 16: 
 17: class Twitter {
 18:     public $access_token;
 19: 
 20:     public function __construct($access_token='') {
 21:         if (is_array($access_token)) {
 22:             $this->access_token = $access_token;
 23:         }
 24:     }
 25: 
 26:     private function _get_request_token () {
 27:         if (isset($_SESSION['oauth_token']) && isset($_SESSION['oauth_token_secret'])) {
 28:             return array(
 29:                 'oauth_token' => $_SESSION['oauth_token'],
 30:                 'oauth_token_secret' => $_SESSION['oauth_token_secret']
 31:             );
 32:         }
 33: 
 34:         throw new \Exception("Request tokens do not exist.");
 35:     }
 36: 
 37:     private function _get_access_token() {
 38:         if (isset($this->access_token)) {
 39:             return $this->access_token;
 40:         }
 41: 
 42:         if (isset($_SESSION['access_token'])) {
 43:             $this->access_token = $_SESSION['access_token'];
 44:             return $this->access_token;
 45:         }
 46: 
 47:         throw new \Exception("Access Token does not exist");
 48:     }
 49: 
 50:     public function is_authorized() {
 51:         try {
 52:             if ($this->_get_access_token()) {
 53:                 return true;
 54:             }
 55:         } catch (\Exception $e) {
 56:             return false;
 57:         }
 58:     }
 59: 
 60:     public function authorize($callback) {
 61:         $connection = new TwitterOAuth(
 62:             TWITTER_CONSUMER_KEY,
 63:             TWITTER_CONSUMER_SECRET
 64:         );
 65: 
 66:         $request_token = $connection->oauth('oauth/request_token', array(
 67:             'oauth_callback' => $callback
 68:         ));
 69: 
 70:         $_SESSION['oauth_token'] = $request_token['oauth_token'];
 71:         $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
 72: 
 73:         $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
 74: 
 75:         header('location: ' . $url);
 76:     }
 77: 
 78:     public function authorize_callback() {
 79:         $this->is_authorized = true;
 80: 
 81:         // The user is now signed in
 82:         $request_token = $this->_get_request_token();
 83: 
 84:         // Ask for the access token
 85:         $connection = new TwitterOAuth(
 86:             TWITTER_CONSUMER_KEY,
 87:             TWITTER_CONSUMER_SECRET,
 88:             $request_token['oauth_token'],
 89:             $request_token['oauth_token_secret']
 90:         );
 91: 
 92:         $_SESSION['access_token'] = $connection->oauth(
 93:             "oauth/access_token",
 94:             array(
 95:                 "oauth_verifier" => $_REQUEST['oauth_verifier']
 96:             )
 97:         );
 98: 
 99:         $this->access_token = $_SESSION['access_token'];
100:     }
101: 
102:     public function get_user() {
103:         return $this->request('account/verify_credentials');
104:     }
105: 
106:     public function get_user_tweets($user) {
107:         return $this->request('statuses/user_timeline', array(
108:             'screen_name' => $user
109:         ));
110:     }
111: 
112:     public function request($url, $options=[]) {
113:         $access_token = $this->_get_access_token();
114: 
115:         $connection = new TwitterOAuth(
116:             TWITTER_CONSUMER_KEY,
117:             TWITTER_CONSUMER_SECRET,
118:             $access_token['oauth_token'],
119:             $access_token['oauth_token_secret']
120:         );
121: 
122:         return $connection->get($url, $options);
123:     }
124: }
sleepyMUSTACHE v.0.8 API documentation generated by ApiGen