1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ritero\SDK\TwitchTV; |
4
|
|
|
|
5
|
|
|
use ritero\SDK\TwitchTV\Methods; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* TwitchTV API SDK for PHP |
9
|
|
|
* |
10
|
|
|
* PHP SDK for interacting with the TwitchTV API |
11
|
|
|
* |
12
|
|
|
* @author Josef Ohnheiser <[email protected]> |
13
|
|
|
* @license https://github.com/jofner/Twitch-SDK/blob/master/LICENSE.md MIT |
14
|
|
|
* @homepage https://github.com/jofner/Twitch-SDK |
15
|
|
|
* @version 2.0.0-dev |
16
|
|
|
* @todo All methods - revisit the setApiVersion() function |
17
|
|
|
*/ |
18
|
|
|
class TwitchSDK |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array|bool |
22
|
|
|
* @todo Create setter and getter with data validation |
23
|
|
|
*/ |
24
|
|
|
protected $authConfig = false; |
25
|
|
|
|
26
|
|
|
/** @var TwitchRequest */ |
27
|
|
|
protected $request; |
28
|
|
|
|
29
|
|
|
/** @var Helper */ |
30
|
|
|
protected $helper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* TwitchAPI URI's |
34
|
|
|
*/ |
35
|
|
|
const URI_USER_FOLLOWS_CHANNEL = '/users/%s/follows/channels'; |
36
|
|
|
const URI_USER_FOLLOW_RELATION = '/users/%s/follows/channels/%s'; |
37
|
|
|
const URI_CHANNEL = 'channels/'; |
38
|
|
|
const URI_CHANNEL_FOLLOWS = 'channels/%s/follows'; |
39
|
|
|
const URI_STREAMS_SEARCH = 'search/streams/'; |
40
|
|
|
const URI_VIDEO = 'videos/'; |
41
|
|
|
const URI_CHAT = 'chat/'; |
42
|
|
|
const URI_CHAT_EMOTICONS = 'chat/emoticons'; |
43
|
|
|
const URI_GAMES_TOP = 'games/top/'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* TwitchSDK constructor |
47
|
|
|
* @param array $config |
48
|
|
|
* @throws TwitchException |
49
|
|
|
*/ |
50
|
|
|
public function __construct(array $config = array()) |
51
|
|
|
{ |
52
|
|
|
if (!in_array('curl', get_loaded_extensions())) { |
53
|
|
|
throw new TwitchException('cURL extension is not installed and is required'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (count($config) > 0) { |
57
|
|
|
$this->setAuthConfig($config); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Develop workaround for requests |
62
|
|
|
* @todo class calls refactoring needed for future use |
63
|
|
|
*/ |
64
|
|
|
$this->request = new TwitchRequest; |
65
|
|
|
$this->helper = new Helper; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* authConfig setter |
70
|
|
|
* @param array $config |
71
|
|
|
* @return TwitchSDK |
72
|
|
|
* @throws TwitchException |
73
|
|
|
*/ |
74
|
|
|
public function setAuthConfig(array $config) |
75
|
|
|
{ |
76
|
|
|
if ($this->configValidate($config) === true) { |
77
|
|
|
$this->authConfig = $config; |
78
|
|
|
} else { |
79
|
|
|
throw new TwitchException('Wrong Twitch API config parameters'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Basic information about the API and authentication status |
87
|
|
|
* @param null $token |
88
|
|
|
* @return \stdClass |
89
|
|
|
* @throws TwitchException |
90
|
|
|
*/ |
91
|
|
|
public function status($token = null) |
92
|
|
|
{ |
93
|
|
|
$auth = null; |
94
|
|
|
|
95
|
|
|
if ($token !== null) { |
96
|
|
|
if ($this->authConfig === false) { |
97
|
|
|
$this->authConfigException(); |
98
|
|
|
} else { |
99
|
|
|
$auth = $this->helper->buildQueryString(array('oauth_token' => $token)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->request->request($auth); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the specified user |
108
|
|
|
* @param $username |
109
|
|
|
* @return \stdClass |
110
|
|
|
* @throws TwitchException |
111
|
|
|
*/ |
112
|
|
|
public function userGet($username) |
113
|
|
|
{ |
114
|
|
|
$user = new Methods\User($this->request); |
115
|
|
|
return $user->getUser($username); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get a user's list of followed channels |
120
|
|
|
* @param $user |
121
|
|
|
* @param null $limit |
122
|
|
|
* @param null $offset |
123
|
|
|
* @return \stdClass |
124
|
|
|
* @throws TwitchException |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function userFollowChannels($user, $limit = null, $offset = null) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$queryString = $this->helper->buildQueryString(array( |
129
|
|
|
'limit' => $limit, |
130
|
|
|
'offset' => $offset, |
131
|
|
|
)); |
132
|
|
|
|
133
|
|
|
return $this->request->request(sprintf(self::URI_USER_FOLLOWS_CHANNEL, $user) . $queryString); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the status of a follow relationship |
138
|
|
|
* @param $user |
139
|
|
|
* @param $channel |
140
|
|
|
* @return \stdClass |
141
|
|
|
* @throws TwitchException |
142
|
|
|
*/ |
143
|
|
|
public function userFollowRelationship($user, $channel) |
144
|
|
|
{ |
145
|
|
|
return $this->request->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set user to follow given channel |
150
|
|
|
* @param $user |
151
|
|
|
* @param $channel |
152
|
|
|
* @param $userToken |
153
|
|
|
* @return \stdClass |
154
|
|
|
* @throws TwitchException |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public function userFollowChannel($user, $channel, $userToken) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$queryString = $this->helper->buildQueryString(array( |
159
|
|
|
'oauth_token' => $userToken, |
160
|
|
|
)); |
161
|
|
|
|
162
|
|
|
return $this->request->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel) . $queryString, 'PUT'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set user to unfollow given channel |
167
|
|
|
* @param $user |
168
|
|
|
* @param $channel |
169
|
|
|
* @param $userToken |
170
|
|
|
* @return \stdClass |
171
|
|
|
* @throws TwitchException |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function userUnfollowChannel($user, $channel, $userToken) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$queryString = $this->helper->buildQueryString(array( |
176
|
|
|
'oauth_token' => $userToken, |
177
|
|
|
)); |
178
|
|
|
|
179
|
|
|
return $this->request->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel) . $queryString, 'DELETE'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the specified channel |
184
|
|
|
* @param $channel |
185
|
|
|
* @return \stdClass |
186
|
|
|
* @throws TwitchException |
187
|
|
|
*/ |
188
|
|
|
public function channelGet($channel) |
189
|
|
|
{ |
190
|
|
|
return $this->request->request(self::URI_CHANNEL . $channel); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the specified team |
195
|
|
|
* @param $teamName |
196
|
|
|
* @return \stdClass |
197
|
|
|
* @throws TwitchException |
198
|
|
|
*/ |
199
|
|
|
public function teamGet($teamName) |
200
|
|
|
{ |
201
|
|
|
$team = new Methods\Team($this->request); |
202
|
|
|
|
203
|
|
|
return $team->getTeam($teamName); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns a list of active teams |
208
|
|
|
* @param integer $limit |
209
|
|
|
* @param integer $offset |
210
|
|
|
* @return \stdClass |
211
|
|
|
* @throws TwitchException |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public function teamList($limit = null, $offset = null) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$queryString = $this->helper->buildQueryString(array( |
216
|
|
|
'limit' => $limit, |
217
|
|
|
'offset' => $offset, |
218
|
|
|
)); |
219
|
|
|
|
220
|
|
|
$team = new Methods\Team($this->request); |
221
|
|
|
|
222
|
|
|
return $team->getTeams($queryString); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get all team members |
227
|
|
|
* @param $teamName |
228
|
|
|
* @return mixed |
229
|
|
|
* @throws TwitchException |
230
|
|
|
*/ |
231
|
|
|
public function teamMembersAll($teamName) |
232
|
|
|
{ |
233
|
|
|
return $this->request->teamRequest($teamName . '/all_channels')->channels; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Returns an array of users who follow the specified channel |
238
|
|
|
* @param $channel |
239
|
|
|
* @param null $limit |
240
|
|
|
* @param null $offset |
241
|
|
|
* @return \stdClass |
242
|
|
|
* @throws TwitchException |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
public function channelFollows($channel, $limit = null, $offset = null) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$queryString = $this->helper->buildQueryString(array( |
247
|
|
|
'limit' => $limit, |
248
|
|
|
'offset' => $offset, |
249
|
|
|
)); |
250
|
|
|
|
251
|
|
|
return $this->request->request(sprintf(self::URI_CHANNEL_FOLLOWS, $channel) . $queryString); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get the specified channel's stream |
256
|
|
|
* @param $channel |
257
|
|
|
* @return \stdClass |
258
|
|
|
* @throws TwitchException |
259
|
|
|
*/ |
260
|
|
|
public function streamGet($channel) |
261
|
|
|
{ |
262
|
|
|
$stream = new Methods\Stream($this->request); |
263
|
|
|
|
264
|
|
|
return $stream->getStream($channel); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Search live streams |
269
|
|
|
* @param $query |
270
|
|
|
* @param null $limit |
271
|
|
|
* @param null $offset |
272
|
|
|
* @return \stdClass |
273
|
|
|
* @throws TwitchException |
274
|
|
|
* @deprecated will be replaced by getStreams() function |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
public function streamSearch($query, $limit = null, $offset = null) |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$queryString = $this->helper->buildQueryString(array( |
279
|
|
|
'query' => $query, |
280
|
|
|
'limit' => $limit, |
281
|
|
|
'offset' => $offset, |
282
|
|
|
)); |
283
|
|
|
|
284
|
|
|
return $this->request->request(self::URI_STREAMS_SEARCH . $queryString); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Summarize streams |
289
|
|
|
* @param null $game |
290
|
|
|
* @param array|null $channels |
291
|
|
|
* @param null $hls |
292
|
|
|
* @return \stdClass |
293
|
|
|
* @throws TwitchException |
294
|
|
|
*/ |
295
|
|
|
public function streamsSummarize($game = null, array $channels = null, $hls = null) |
296
|
|
|
{ |
297
|
|
|
if (!empty($channels)) { |
298
|
|
|
$channels = implode(',', $channels); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$queryString = $this->helper->buildQueryString(array( |
302
|
|
|
'game' => $game, |
303
|
|
|
'channel' => $channels, |
304
|
|
|
'hls' => $hls, |
305
|
|
|
)); |
306
|
|
|
|
307
|
|
|
$stream = new Methods\Stream($this->request); |
308
|
|
|
|
309
|
|
|
return $stream->getSummary($queryString); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get featured streams |
314
|
|
|
* @param null $limit |
315
|
|
|
* @param null $offset |
316
|
|
|
* @param null $hls |
317
|
|
|
* @return \stdClass |
318
|
|
|
* @throws TwitchException |
319
|
|
|
*/ |
320
|
|
|
public function streamsFeatured($limit = null, $offset = null, $hls = null) |
321
|
|
|
{ |
322
|
|
|
$queryString = $this->helper->buildQueryString(array( |
323
|
|
|
'limit' => $limit, |
324
|
|
|
'offset' => $offset, |
325
|
|
|
'hls' => $hls, |
326
|
|
|
)); |
327
|
|
|
|
328
|
|
|
$stream = new Methods\Stream($this->request); |
329
|
|
|
|
330
|
|
|
return $stream->getFeatured($queryString); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get streams by channel |
335
|
|
|
* @param $channels |
336
|
|
|
* @param null $limit |
337
|
|
|
* @param null $offset |
338
|
|
|
* @param null $embeddable |
339
|
|
|
* @param null $hls |
340
|
|
|
* @return \stdClass |
341
|
|
|
* @deprecated will be replaced by getStreams() function |
342
|
|
|
*/ |
343
|
|
|
public function streamsByChannels($channels, $limit = null, $offset = null, $embeddable = null, $hls = null) |
344
|
|
|
{ |
345
|
|
|
$channelsString = implode(',', $channels); |
346
|
|
|
|
347
|
|
|
return $this->getStreams(null, $limit, $offset, $channelsString, $embeddable, $hls); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get streams by game |
352
|
|
|
* @param $game |
353
|
|
|
* @param null $limit |
354
|
|
|
* @param null $offset |
355
|
|
|
* @param null $embeddable |
356
|
|
|
* @param null $hls |
357
|
|
|
* @return \stdClass |
358
|
|
|
* @deprecated will be replaced by getStreams() function |
359
|
|
|
*/ |
360
|
|
|
public function streamsByGame($game, $limit = null, $offset = null, $embeddable = null, $hls = null) |
361
|
|
|
{ |
362
|
|
|
return $this->getStreams($game, $limit, $offset, null, $embeddable, $hls); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Get video |
367
|
|
|
* @param $video |
368
|
|
|
* @return \stdClass |
369
|
|
|
* @throws TwitchException |
370
|
|
|
*/ |
371
|
|
|
public function videoGet($video) |
372
|
|
|
{ |
373
|
|
|
return $this->request->request(self::URI_VIDEO . $video); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Get videos for a channel |
378
|
|
|
* @param $channel |
379
|
|
|
* @param null $limit |
380
|
|
|
* @param null $offset |
381
|
|
|
* @return \stdClass |
382
|
|
|
* @throws TwitchException |
383
|
|
|
*/ |
384
|
|
|
public function videosByChannel($channel, $limit = null, $offset = null) |
385
|
|
|
{ |
386
|
|
|
$queryString = $this->helper->buildQueryString(array( |
387
|
|
|
'limit' => $limit, |
388
|
|
|
'offset' => $offset, |
389
|
|
|
)); |
390
|
|
|
|
391
|
|
|
return $this->request->request(self::URI_CHANNEL . $channel . '/' . self::URI_VIDEO . $queryString); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get the specified channel's chat |
396
|
|
|
* @param $channel |
397
|
|
|
* @return \stdClass |
398
|
|
|
* @throws TwitchException |
399
|
|
|
*/ |
400
|
|
|
public function chatGet($channel) |
401
|
|
|
{ |
402
|
|
|
return $this->request->request(self::URI_CHAT . $channel); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Get a chat's emoticons |
407
|
|
|
* @return \stdClass |
408
|
|
|
* @throws TwitchException |
409
|
|
|
*/ |
410
|
|
|
public function chatEmoticons() |
411
|
|
|
{ |
412
|
|
|
return $this->request->request(self::URI_CHAT_EMOTICONS); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Get top games |
417
|
|
|
* @param null $limit |
418
|
|
|
* @param null $offset |
419
|
|
|
* @return \stdClass |
420
|
|
|
* @throws TwitchException |
421
|
|
|
*/ |
422
|
|
View Code Duplication |
public function gamesTop($limit = null, $offset = null) |
|
|
|
|
423
|
|
|
{ |
424
|
|
|
$queryString = $this->helper->buildQueryString(array( |
425
|
|
|
'limit' => $limit, |
426
|
|
|
'offset' => $offset, |
427
|
|
|
)); |
428
|
|
|
|
429
|
|
|
return $this->request->request(self::URI_GAMES_TOP . $queryString); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Get HTML code for stream embedding |
434
|
|
|
* @param $channel |
435
|
|
|
* @param int $width |
436
|
|
|
* @param int $height |
437
|
|
|
* @param int $volume |
438
|
|
|
* @return string |
439
|
|
|
*/ |
440
|
|
View Code Duplication |
public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
|
|
|
441
|
|
|
{ |
442
|
|
|
return '<object type="application/x-shockwave-flash" |
443
|
|
|
height="' . $height . '" |
444
|
|
|
width="' . $width . '" |
445
|
|
|
id="live_embed_player_flash" |
446
|
|
|
data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' . $channel . '" |
447
|
|
|
bgcolor="#000000"> |
448
|
|
|
<param name="allowFullScreen" |
449
|
|
|
value="true" /> |
450
|
|
|
<param name="allowScriptAccess" |
451
|
|
|
value="always" /> |
452
|
|
|
<param name="allowNetworking" |
453
|
|
|
value="all" /> |
454
|
|
|
<param name="movie" |
455
|
|
|
value="http://www.twitch.tv/widgets/live_embed_player.swf" /> |
456
|
|
|
<param name="flashvars" |
457
|
|
|
value="hostname=www.twitch.tv&channel=' . $channel . '&auto_play=true&start_volume=' . $volume . '" /> |
458
|
|
|
</object>'; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Get HTML code for video embedding |
463
|
|
|
* @param $channel |
464
|
|
|
* @param $chapterid |
465
|
|
|
* @param int $width |
466
|
|
|
* @param int $height |
467
|
|
|
* @param int $volume |
468
|
|
|
* @return string |
469
|
|
|
*/ |
470
|
|
View Code Duplication |
public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
|
|
|
|
471
|
|
|
{ |
472
|
|
|
return '<object bgcolor="#000000" |
473
|
|
|
data="http://www.twitch.tv/widgets/archive_embed_player.swf" |
474
|
|
|
width="' . $width . '" |
475
|
|
|
height="' . $height . '" |
476
|
|
|
id="clip_embed_player_flash" |
477
|
|
|
type="application/x-shockwave-flash"> |
478
|
|
|
<param name="movie" |
479
|
|
|
value="http://www.twitch.tv/widgets/archive_embed_player.swf" /> |
480
|
|
|
<param name="allowScriptAccess" |
481
|
|
|
value="always" /> |
482
|
|
|
<param name="allowNetworking" |
483
|
|
|
value="all" /> |
484
|
|
|
<param name="allowFullScreen" |
485
|
|
|
value="true" /> |
486
|
|
|
<param name="flashvars" |
487
|
|
|
value="channel=' . $channel . '&start_volume=' . $volume . '&auto_play=false&chapter_id=' . $chapterid . '" /> |
488
|
|
|
</object>'; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Get HTML code for chat embedding |
493
|
|
|
* @param $channel |
494
|
|
|
* @param int $width |
495
|
|
|
* @param int $height |
496
|
|
|
* @return string |
497
|
|
|
*/ |
498
|
|
|
public function embedChat($channel, $width = 400, $height = 300) |
499
|
|
|
{ |
500
|
|
|
return '<iframe frameborder="0" |
501
|
|
|
scrolling="no" |
502
|
|
|
id="chat_embed" |
503
|
|
|
src="http://twitch.tv/chat/embed?channel=' . $channel . '&popout_chat=true" |
504
|
|
|
height="' . $height . '" |
505
|
|
|
width="' . $width . '"> |
506
|
|
|
</iframe>'; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Get login URL for authentication |
511
|
|
|
* @param string $scope Specify which permissions your app requires (space separated list) |
512
|
|
|
* @return string |
513
|
|
|
* @throws TwitchException |
514
|
|
|
*/ |
515
|
|
View Code Duplication |
public function authLoginURL($scope) |
|
|
|
|
516
|
|
|
{ |
517
|
|
|
if ($this->authConfig === false) { |
518
|
|
|
$this->authConfigException(); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
$queryString = $this->helper->buildQueryString(array( |
522
|
|
|
'response_type' => 'code', |
523
|
|
|
'client_id' => $this->authConfig['client_id'], |
524
|
|
|
'redirect_uri' => $this->authConfig['redirect_uri'], |
525
|
|
|
'scope' => $scope, |
526
|
|
|
)); |
527
|
|
|
|
528
|
|
|
$auth = new Methods\Auth; |
529
|
|
|
|
530
|
|
|
return $auth->getLoginURL($queryString); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Get authentication access token |
535
|
|
|
* @param string $code returned after app authorization by user |
536
|
|
|
* @return \stdClass |
537
|
|
|
* @throws TwitchException |
538
|
|
|
*/ |
539
|
|
View Code Duplication |
public function authAccessTokenGet($code) |
|
|
|
|
540
|
|
|
{ |
541
|
|
|
if ($this->authConfig === false) { |
542
|
|
|
$this->authConfigException(); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
$queryString = $this->helper->buildQueryString(array( |
546
|
|
|
'client_id' => $this->authConfig['client_id'], |
547
|
|
|
'client_secret' => $this->authConfig['client_secret'], |
548
|
|
|
'grant_type' => 'authorization_code', |
549
|
|
|
'redirect_uri' => $this->authConfig['redirect_uri'], |
550
|
|
|
'code' => $code, |
551
|
|
|
)); |
552
|
|
|
|
553
|
|
|
$auth = new Methods\Auth; |
554
|
|
|
|
555
|
|
|
return $auth->getAccessToken($queryString); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Get the authenticated user |
560
|
|
|
* - requires scope 'user_read' |
561
|
|
|
* @param string |
562
|
|
|
* @return \stdClass |
563
|
|
|
* @throws TwitchException |
564
|
|
|
*/ |
565
|
|
View Code Duplication |
public function authUserGet($token) |
|
|
|
|
566
|
|
|
{ |
567
|
|
|
if ($this->authConfig === false) { |
568
|
|
|
$this->authConfigException(); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
$queryString = $this->helper->buildQueryString(array( |
572
|
|
|
'oauth_token' => $token, |
573
|
|
|
'client_id' => $this->authConfig['client_id'], |
574
|
|
|
)); |
575
|
|
|
|
576
|
|
|
$user = new Methods\User($this->request); |
577
|
|
|
return $user->getUserAuth($queryString); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* Get the authenticated channel |
582
|
|
|
* - requires scope 'channel_read' |
583
|
|
|
* @param string |
584
|
|
|
* @return \stdClass |
585
|
|
|
* @throws TwitchException |
586
|
|
|
*/ |
587
|
|
View Code Duplication |
public function authChannelGet($token) |
|
|
|
|
588
|
|
|
{ |
589
|
|
|
if ($this->authConfig === false) { |
590
|
|
|
$this->authConfigException(); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
$queryString = $this->helper->buildQueryString(array( |
594
|
|
|
'oauth_token' => $token, |
595
|
|
|
'client_id' => $this->authConfig['client_id'], |
596
|
|
|
)); |
597
|
|
|
|
598
|
|
|
$channels = new Methods\Channel; |
599
|
|
|
|
600
|
|
|
return $channels->getChannel($queryString); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Returns an array of users who are editors of specified channel |
605
|
|
|
* - requires scope 'channel_read' |
606
|
|
|
* @param string |
607
|
|
|
* @param string |
608
|
|
|
* @return \stdClass |
609
|
|
|
* @throws TwitchException |
610
|
|
|
*/ |
611
|
|
View Code Duplication |
public function authChannelEditors($token, $channel) |
|
|
|
|
612
|
|
|
{ |
613
|
|
|
if ($this->authConfig === false) { |
614
|
|
|
$this->authConfigException(); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
$queryString = $this->helper->buildQueryString(array( |
618
|
|
|
'oauth_token' => $token, |
619
|
|
|
'client_id' => $this->authConfig['client_id'], |
620
|
|
|
)); |
621
|
|
|
|
622
|
|
|
$channels = new Methods\Channel; |
623
|
|
|
|
624
|
|
|
$channels->getEditors($channel, $queryString); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Returns an array of subscriptions who are subscribed to specified channel |
629
|
|
|
* - requires scope 'channel_subscriptions' |
630
|
|
|
* @param string $token - user's access token |
631
|
|
|
* @param string $channel |
632
|
|
|
* @param integer $limit - can be up to 100 |
633
|
|
|
* @param integer $offset |
634
|
|
|
* @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
635
|
|
|
* @return \stdClass |
636
|
|
|
* @throws TwitchException |
637
|
|
|
*/ |
638
|
|
View Code Duplication |
public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
|
|
|
639
|
|
|
{ |
640
|
|
|
if ($this->authConfig === false) { |
641
|
|
|
$this->authConfigException(); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
$queryString = $this->helper->buildQueryString(array( |
645
|
|
|
'oauth_token' => $token, |
646
|
|
|
'client_id' => $this->authConfig['client_id'], |
647
|
|
|
'direction' => $direction, |
648
|
|
|
'limit' => $limit, |
649
|
|
|
'offset' => $offset |
650
|
|
|
)); |
651
|
|
|
|
652
|
|
|
$channels = new Methods\Subscription; |
653
|
|
|
|
654
|
|
|
$channels->getSubscriptions($channel, $queryString); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* List the live streams that the authenticated user is following |
659
|
|
|
* - requires scope 'user_read' |
660
|
|
|
* @param string |
661
|
|
|
* @param integer $limit |
662
|
|
|
* @param integer $offset |
663
|
|
|
* @param bool $hls |
664
|
|
|
* @return \stdClass |
665
|
|
|
* @throws TwitchException |
666
|
|
|
*/ |
667
|
|
View Code Duplication |
public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
|
|
|
668
|
|
|
{ |
669
|
|
|
if ($this->authConfig === false) { |
670
|
|
|
$this->authConfigException(); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
$queryString = $this->helper->buildQueryString(array( |
674
|
|
|
'oauth_token' => $token, |
675
|
|
|
'client_id' => $this->authConfig['client_id'], |
676
|
|
|
'limit' => $limit, |
677
|
|
|
'offset' => $offset, |
678
|
|
|
'hls' => $hls, |
679
|
|
|
)); |
680
|
|
|
|
681
|
|
|
$user = new Methods\User($this->request); |
682
|
|
|
return $user->getFollowedStreams($queryString); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* Get streams helper |
687
|
|
|
* @param null $game |
688
|
|
|
* @param null $limit |
689
|
|
|
* @param null $offset |
690
|
|
|
* @param null $channels |
691
|
|
|
* @param null $embeddable |
692
|
|
|
* @param null $hls |
693
|
|
|
* @return \stdClass |
694
|
|
|
* @throws TwitchException |
695
|
|
|
*/ |
696
|
|
|
public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null) |
697
|
|
|
{ |
698
|
|
|
$params = array( |
699
|
|
|
'game' => $game, |
700
|
|
|
'limit' => $limit, |
701
|
|
|
'offset' => $offset, |
702
|
|
|
'channel' => !empty($channels) ? $channels : null, |
703
|
|
|
'embeddable' => $embeddable, |
704
|
|
|
'hls' => $hls, |
705
|
|
|
); |
706
|
|
|
|
707
|
|
|
$queryString = $this->helper->buildQueryString($params); |
708
|
|
|
$stream = new Methods\Stream($this->request); |
709
|
|
|
|
710
|
|
|
return $stream->getStreams($queryString); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Validate parameters for authentication |
715
|
|
|
* @param array |
716
|
|
|
* @return boolean |
717
|
|
|
*/ |
718
|
|
|
private function configValidate($config) |
719
|
|
|
{ |
720
|
|
|
$check = array('client_id', 'client_secret', 'redirect_uri'); |
721
|
|
|
|
722
|
|
|
foreach ($check AS $val) { |
723
|
|
|
if (!array_key_exists($val, $config) || |
724
|
|
|
(empty($config[$val]) || |
725
|
|
|
!is_string($config[$val])) |
726
|
|
|
) { |
727
|
|
|
return false; |
728
|
|
|
} |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
return true; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* Configuration exception |
736
|
|
|
* @throws TwitchException |
737
|
|
|
*/ |
738
|
|
|
private function authConfigException() |
739
|
|
|
{ |
740
|
|
|
throw new TwitchException('Cannot call authenticate functions without valid API configuration'); |
741
|
|
|
} |
742
|
|
|
} |
743
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.