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