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