|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Integrations\Connectors\Twitter; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Abraham Williams ([email protected]) http://abrah.am |
|
7
|
|
|
* |
|
8
|
|
|
* The first PHP Library to support OAuth for Twitter's REST API. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/* Load OAuth lib. You can find it at http://oauth.net */ |
|
12
|
|
|
require_once 'OAuth.php'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Twitter OAuth class |
|
16
|
|
|
*/ |
|
17
|
|
|
class TwitterOAuth |
|
18
|
|
|
{ |
|
19
|
|
|
/* Contains the last HTTP status code returned. */ |
|
20
|
|
|
public $http_code; |
|
21
|
|
|
/* Contains the last API call. */ |
|
22
|
|
|
public $url; |
|
23
|
|
|
/* Set up the API root URL. */ |
|
24
|
|
|
public $host = "https://api.twitter.com/1.1/"; |
|
25
|
|
|
/* Set timeout default. */ |
|
26
|
|
|
public $timeout = 30; |
|
27
|
|
|
/* Set connect timeout. */ |
|
28
|
|
|
public $connecttimeout = 30; |
|
29
|
|
|
/* Verify SSL Cert. */ |
|
30
|
|
|
public $ssl_verifypeer = false; |
|
31
|
|
|
/* Respons format. */ |
|
32
|
|
|
public $format = 'json'; |
|
33
|
|
|
/* Decode returned json data. */ |
|
34
|
|
|
public $decode_json = true; |
|
35
|
|
|
/* Contains the last HTTP headers returned. */ |
|
36
|
|
|
public $http_info; |
|
37
|
|
|
/* Set the useragnet. */ |
|
38
|
|
|
public $useragent = 'TwitterOAuth v0.2.0-beta2'; |
|
39
|
|
|
/* Immediately retry the API call if the response was not successful. */ |
|
40
|
|
|
//public $retry = TRUE; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set API URLS |
|
47
|
|
|
*/ |
|
48
|
|
|
function accessTokenURL() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
return 'https://api.twitter.com/oauth/access_token'; |
|
51
|
|
|
} |
|
52
|
|
|
function authenticateURL() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
return 'https://api.twitter.com/oauth/authenticate'; |
|
55
|
|
|
} |
|
56
|
|
|
function authorizeURL() |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
return 'https://api.twitter.com/oauth/authorize'; |
|
59
|
|
|
} |
|
60
|
|
|
function requestTokenURL() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
return 'https://api.twitter.com/oauth/request_token'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Debug helpers |
|
67
|
|
|
*/ |
|
68
|
|
|
function lastStatusCode() |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
return $this->http_status; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
function lastAPICall() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
return $this->last_api_call; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* construct TwitterOAuth object |
|
79
|
|
|
*/ |
|
80
|
|
|
function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1(); |
|
|
|
|
|
|
83
|
|
|
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret); |
|
|
|
|
|
|
84
|
|
|
if (!empty($oauth_token) && !empty($oauth_token_secret)) { |
|
85
|
|
|
$this->token = new OAuthConsumer($oauth_token, $oauth_token_secret); |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
$this->token = null; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get a request_token from Twitter |
|
94
|
|
|
* |
|
95
|
|
|
* @returns a key/value array containing oauth_token and oauth_token_secret |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
function getRequestToken($oauth_callback) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$parameters = array(); |
|
100
|
|
|
$parameters['oauth_callback'] = $oauth_callback; |
|
101
|
|
|
$request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters); |
|
102
|
|
|
$token = OAuthUtil::parse_parameters($request); |
|
103
|
|
|
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); |
|
104
|
|
|
return $token; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the authorize URL |
|
109
|
|
|
* |
|
110
|
|
|
* @returns a string |
|
111
|
|
|
*/ |
|
112
|
|
|
function getAuthorizeURL($token, $sign_in_with_twitter = true) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
if (is_array($token)) { |
|
115
|
|
|
$token = $token['oauth_token']; |
|
116
|
|
|
} |
|
117
|
|
|
if (empty($sign_in_with_twitter)) { |
|
118
|
|
|
return $this->authorizeURL() . "?oauth_token={$token}"; |
|
119
|
|
|
} else { |
|
120
|
|
|
return $this->authenticateURL() . "?oauth_token={$token}"; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Exchange request token and secret for an access token and |
|
126
|
|
|
* secret, to sign API calls. |
|
127
|
|
|
* |
|
128
|
|
|
* @returns array("oauth_token" => "the-access-token", |
|
129
|
|
|
* "oauth_token_secret" => "the-access-secret", |
|
130
|
|
|
* "user_id" => "9436992", |
|
131
|
|
|
* "screen_name" => "abraham") |
|
132
|
|
|
*/ |
|
133
|
|
View Code Duplication |
function getAccessToken($oauth_verifier) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
$parameters = array(); |
|
136
|
|
|
$parameters['oauth_verifier'] = $oauth_verifier; |
|
137
|
|
|
$request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters); |
|
138
|
|
|
$token = OAuthUtil::parse_parameters($request); |
|
139
|
|
|
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); |
|
140
|
|
|
return $token; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* One time exchange of username and password for access token and secret. |
|
145
|
|
|
* |
|
146
|
|
|
* @returns array("oauth_token" => "the-access-token", |
|
147
|
|
|
* "oauth_token_secret" => "the-access-secret", |
|
148
|
|
|
* "user_id" => "9436992", |
|
149
|
|
|
* "screen_name" => "abraham", |
|
150
|
|
|
* "x_auth_expires" => "0") |
|
151
|
|
|
*/ |
|
152
|
|
|
function getXAuthToken($username, $password) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
$parameters = array(); |
|
155
|
|
|
$parameters['x_auth_username'] = $username; |
|
156
|
|
|
$parameters['x_auth_password'] = $password; |
|
157
|
|
|
$parameters['x_auth_mode'] = 'client_auth'; |
|
158
|
|
|
$request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters); |
|
159
|
|
|
$token = OAuthUtil::parse_parameters($request); |
|
160
|
|
|
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']); |
|
161
|
|
|
return $token; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* GET wrapper for oAuthRequest. |
|
166
|
|
|
*/ |
|
167
|
|
View Code Duplication |
function get($url, $parameters = array()) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$response = $this->oAuthRequest($url, 'GET', $parameters); |
|
170
|
|
|
if ($this->format === 'json' && $this->decode_json) { |
|
171
|
|
|
return json_decode($response); |
|
172
|
|
|
} |
|
173
|
|
|
return $response; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* POST wrapper for oAuthRequest. |
|
178
|
|
|
*/ |
|
179
|
|
View Code Duplication |
function post($url, $parameters = array()) |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
$response = $this->oAuthRequest($url, 'POST', $parameters); |
|
182
|
|
|
if ($this->format === 'json' && $this->decode_json) { |
|
183
|
|
|
return json_decode($response); |
|
184
|
|
|
} |
|
185
|
|
|
return $response; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* DELETE wrapper for oAuthReqeust. |
|
190
|
|
|
*/ |
|
191
|
|
View Code Duplication |
function delete($url, $parameters = array()) |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
$response = $this->oAuthRequest($url, 'DELETE', $parameters); |
|
194
|
|
|
if ($this->format === 'json' && $this->decode_json) { |
|
195
|
|
|
return json_decode($response); |
|
196
|
|
|
} |
|
197
|
|
|
return $response; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Format and sign an OAuth / API request |
|
202
|
|
|
*/ |
|
203
|
|
|
function oAuthRequest($url, $method, $parameters) |
|
|
|
|
|
|
204
|
|
|
{ |
|
205
|
|
|
if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { |
|
206
|
|
|
$url = "{$this->host}{$url}.{$this->format}"; |
|
207
|
|
|
} |
|
208
|
|
|
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); |
|
209
|
|
|
$request->sign_request($this->sha1_method, $this->consumer, $this->token); |
|
210
|
|
|
switch ($method) { |
|
211
|
|
|
case 'GET': |
|
212
|
|
|
return $this->http($request->to_url(), 'GET'); |
|
213
|
|
|
default: |
|
214
|
|
|
return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata()); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Make an HTTP request |
|
220
|
|
|
* |
|
221
|
|
|
* @return API results |
|
222
|
|
|
*/ |
|
223
|
|
|
function http($url, $method, $postfields = null) |
|
|
|
|
|
|
224
|
|
|
{ |
|
225
|
|
|
$this->http_info = array(); |
|
226
|
|
|
$ci = curl_init(); |
|
227
|
|
|
/* Curl settings */ |
|
228
|
|
|
curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent); |
|
229
|
|
|
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); |
|
230
|
|
|
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); |
|
231
|
|
|
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); |
|
232
|
|
|
curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); |
|
233
|
|
|
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); |
|
234
|
|
|
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); |
|
235
|
|
|
curl_setopt($ci, CURLOPT_HEADER, false); |
|
236
|
|
|
|
|
237
|
|
|
switch ($method) { |
|
238
|
|
|
case 'POST': |
|
239
|
|
|
curl_setopt($ci, CURLOPT_POST, true); |
|
240
|
|
|
if (!empty($postfields)) { |
|
241
|
|
|
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); |
|
242
|
|
|
} |
|
243
|
|
|
break; |
|
244
|
|
|
case 'DELETE': |
|
245
|
|
|
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
|
246
|
|
|
if (!empty($postfields)) { |
|
247
|
|
|
$url = "{$url}?{$postfields}"; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
curl_setopt($ci, CURLOPT_URL, $url); |
|
252
|
|
|
$response = curl_exec($ci); |
|
253
|
|
|
$this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); |
|
254
|
|
|
$this->http_info = array_merge($this->http_info, curl_getinfo($ci)); |
|
255
|
|
|
$this->url = $url; |
|
256
|
|
|
curl_close($ci); |
|
257
|
|
|
return $response; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Get the header info to store. |
|
262
|
|
|
*/ |
|
263
|
|
|
function getHeader($ch, $header) |
|
|
|
|
|
|
264
|
|
|
{ |
|
265
|
|
|
$i = strpos($header, ':'); |
|
266
|
|
|
if (!empty($i)) { |
|
267
|
|
|
$key = str_replace('-', '_', strtolower(substr($header, 0, $i))); |
|
268
|
|
|
$value = trim(substr($header, $i + 2)); |
|
269
|
|
|
$this->http_header[$key] = $value; |
|
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
return strlen($header); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.