1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace jofner\SDK\TwitchTV; |
4
|
|
|
|
5
|
|
|
use jofner\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
|
|
|
/** @var array */ |
20
|
|
|
private $config = array(); |
21
|
|
|
|
22
|
|
|
/** @var TwitchRequest */ |
23
|
|
|
protected $request; |
24
|
|
|
|
25
|
|
|
/** @var Helper */ |
26
|
|
|
protected $helper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* TwitchSDK constructor |
30
|
|
|
* @param array $config |
31
|
|
|
* @throws TwitchException |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $config = array()) |
34
|
|
|
{ |
35
|
|
|
if (!in_array('curl', get_loaded_extensions(), true)) { |
36
|
|
|
throw new TwitchException('cURL extension is not installed and is required'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (count($config) > 0) { |
40
|
|
|
$this->setConfig($config); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->request = new TwitchRequest; |
44
|
|
|
$this->helper = new Helper; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* config setter |
49
|
|
|
* @param array $config |
50
|
|
|
* @return TwitchSDK |
51
|
|
|
* @throws TwitchException |
52
|
|
|
*/ |
53
|
|
|
public function setConfig(array $config) |
54
|
|
|
{ |
55
|
|
|
if ($this->configValidate($config) === true) { |
56
|
|
|
$this->config = $config; |
57
|
|
|
} else { |
58
|
|
|
throw new TwitchException('Wrong Twitch API config parameters'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if config is set |
66
|
|
|
* @throws TwitchException |
67
|
|
|
*/ |
68
|
|
|
private function checkConfig() |
69
|
|
|
{ |
70
|
|
|
if (count($this->config) === 0) { |
71
|
|
|
$this->configException(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get value from config |
77
|
|
|
* @param string $key |
78
|
|
|
* @return mixed |
79
|
|
|
* @throws TwitchException |
80
|
|
|
*/ |
81
|
|
|
private function getConfigParam($key) |
82
|
|
|
{ |
83
|
|
|
if (!array_key_exists($key, $this->config)) { |
84
|
|
|
throw new TwitchException('Missing configuration parameter'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->config[$key]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Basic information about the API and authentication status |
92
|
|
|
* @param null $token |
93
|
|
|
* @return \stdClass |
94
|
|
|
* @throws TwitchException |
95
|
|
|
*/ |
96
|
|
|
public function status($token = null) |
97
|
|
|
{ |
98
|
|
|
$auth = null; |
99
|
|
|
|
100
|
|
|
if ($token !== null) { |
101
|
|
|
if (count($this->config) === 0) { |
102
|
|
|
$this->configException(); |
103
|
|
|
} else { |
104
|
|
|
$auth = $this->helper->buildQueryString(array('oauth_token' => $token)); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->request->request($auth); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the specified user |
113
|
|
|
* @param $username |
114
|
|
|
* @return \stdClass |
115
|
|
|
* @throws TwitchException |
116
|
|
|
*/ |
117
|
|
|
public function userGet($username) |
118
|
|
|
{ |
119
|
|
|
$user = new Methods\User($this->request); |
120
|
|
|
|
121
|
|
|
return $user->getUser($username); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a user's list of followed channels |
126
|
|
|
* @param string $user |
127
|
|
|
* @param integer $limit |
128
|
|
|
* @param integer $offset |
129
|
|
|
* @param string $direction |
130
|
|
|
* @param string $sortby |
131
|
|
|
* @return \stdClass |
132
|
|
|
* @throws TwitchException |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null) |
135
|
|
|
{ |
136
|
|
|
$queryString = $this->helper->buildQueryString(array( |
137
|
|
|
'limit' => $limit, |
138
|
|
|
'offset' => $offset, |
139
|
|
|
'direction' => $direction, |
140
|
|
|
'sortby' => $sortby, |
141
|
|
|
)); |
142
|
|
|
|
143
|
|
|
$follow = new Methods\Follow($this->request); |
144
|
|
|
|
145
|
|
|
return $follow->userFollowChannels($user, $queryString); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the status of a follow relationship |
150
|
|
|
* @param string $user |
151
|
|
|
* @param string $channel |
152
|
|
|
* @return \stdClass |
153
|
|
|
* @throws TwitchException |
154
|
|
|
*/ |
155
|
|
|
public function userFollowRelationship($user, $channel) |
156
|
|
|
{ |
157
|
|
|
$follow = new Methods\Follow($this->request); |
158
|
|
|
|
159
|
|
|
return $follow->userIsFollowingChannel($user, $channel); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set user to follow given channel |
164
|
|
|
* - requires scope 'user_follows_edit' |
165
|
|
|
* @param string $user |
166
|
|
|
* @param string $channel |
167
|
|
|
* @param string $userToken |
168
|
|
|
* @param bool $notifications |
169
|
|
|
* @return \stdClass |
170
|
|
|
* @throws TwitchException |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
public function authUserFollowChannel($user, $channel, $userToken, $notifications = false) |
173
|
|
|
{ |
174
|
|
|
$this->checkConfig(); |
175
|
|
|
|
176
|
|
|
$queryString = $this->helper->buildQueryString(array( |
177
|
|
|
'oauth_token' => $userToken, |
178
|
|
|
'notifications' => $notifications, |
179
|
|
|
)); |
180
|
|
|
|
181
|
|
|
$follow = new Methods\Follow($this->request); |
182
|
|
|
|
183
|
|
|
return $follow->followChannel($user, $channel, $queryString); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set user to unfollow given channel |
188
|
|
|
* - requires scope 'user_follows_edit' |
189
|
|
|
* @param string $user |
190
|
|
|
* @param string $channel |
191
|
|
|
* @param string $userToken |
192
|
|
|
* @return \stdClass |
193
|
|
|
* @throws TwitchException |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
public function authUserUnfollowChannel($user, $channel, $userToken) |
196
|
|
|
{ |
197
|
|
|
$this->checkConfig(); |
198
|
|
|
|
199
|
|
|
$queryString = $this->helper->buildQueryString(array( |
200
|
|
|
'oauth_token' => $userToken, |
201
|
|
|
)); |
202
|
|
|
|
203
|
|
|
$follow = new Methods\Follow($this->request); |
204
|
|
|
|
205
|
|
|
return $follow->unfollowChannel($user, $channel, $queryString); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the specified channel |
210
|
|
|
* @param string $channelName |
211
|
|
|
* @return \stdClass |
212
|
|
|
* @throws TwitchException |
213
|
|
|
*/ |
214
|
|
|
public function channelGet($channelName) |
215
|
|
|
{ |
216
|
|
|
$channel = new Methods\Channel($this->request); |
217
|
|
|
|
218
|
|
|
return $channel->getChannels($channelName); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Return team list for specified channel |
223
|
|
|
* @param string $channelName |
224
|
|
|
* @return \stdClass |
225
|
|
|
* @throws TwitchException |
226
|
|
|
*/ |
227
|
|
|
public function channelTeamsGet($channelName) |
228
|
|
|
{ |
229
|
|
|
$channel = new Methods\Channel($this->request); |
230
|
|
|
|
231
|
|
|
return $channel->getTeams($channelName); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Search channels |
236
|
|
|
* @param string $query |
237
|
|
|
* @param integer $limit |
238
|
|
|
* @param integer $offset |
239
|
|
|
* @return \stdClass |
240
|
|
|
* @throws TwitchException |
241
|
|
|
*/ |
242
|
|
View Code Duplication |
public function channelSearch($query, $limit = null, $offset = null) |
243
|
|
|
{ |
244
|
|
|
$queryString = $this->helper->buildQueryString(array( |
245
|
|
|
'query' => $query, |
246
|
|
|
'limit' => $limit, |
247
|
|
|
'offset' => $offset, |
248
|
|
|
)); |
249
|
|
|
|
250
|
|
|
$search = new Methods\Search($this->request); |
251
|
|
|
|
252
|
|
|
return $search->channels($queryString); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the specified team |
257
|
|
|
* @param $teamName |
258
|
|
|
* @return \stdClass |
259
|
|
|
* @throws TwitchException |
260
|
|
|
*/ |
261
|
|
|
public function teamGet($teamName) |
262
|
|
|
{ |
263
|
|
|
$team = new Methods\Team($this->request); |
264
|
|
|
|
265
|
|
|
return $team->getTeam($teamName); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Returns a list of active teams |
270
|
|
|
* @param integer $limit |
271
|
|
|
* @param integer $offset |
272
|
|
|
* @return \stdClass |
273
|
|
|
* @throws TwitchException |
274
|
|
|
*/ |
275
|
|
View Code Duplication |
public function teamList($limit = null, $offset = null) |
276
|
|
|
{ |
277
|
|
|
$queryString = $this->helper->buildQueryString(array( |
278
|
|
|
'limit' => $limit, |
279
|
|
|
'offset' => $offset, |
280
|
|
|
)); |
281
|
|
|
|
282
|
|
|
$team = new Methods\Team($this->request); |
283
|
|
|
|
284
|
|
|
return $team->getTeams($queryString); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get all team members |
289
|
|
|
* @param $teamName |
290
|
|
|
* @return mixed |
291
|
|
|
* @throws TwitchException |
292
|
|
|
*/ |
293
|
|
|
public function teamMembersAll($teamName) |
294
|
|
|
{ |
295
|
|
|
return $this->request->teamRequest($teamName . '/all_channels')->channels; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Returns an array of users who follow the specified channel |
300
|
|
|
* @param string $channelName |
301
|
|
|
* @param integer $limit |
302
|
|
|
* @param integer $offset |
303
|
|
|
* @param string $cursor |
304
|
|
|
* @param string $direction |
305
|
|
|
* @return \stdClass |
306
|
|
|
* @throws TwitchException |
307
|
|
|
*/ |
308
|
|
View Code Duplication |
public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
309
|
|
|
{ |
310
|
|
|
$queryString = $this->helper->buildQueryString(array( |
311
|
|
|
'limit' => $limit, |
312
|
|
|
'offset' => $offset, |
313
|
|
|
'cursor' => $cursor, |
314
|
|
|
'direction' => $direction, |
315
|
|
|
)); |
316
|
|
|
|
317
|
|
|
$follow = new Methods\Follow($this->request); |
318
|
|
|
|
319
|
|
|
return $follow->getChannelFollows($channelName, $queryString); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Get the specified channel's stream |
324
|
|
|
* @param $channel |
325
|
|
|
* @return \stdClass |
326
|
|
|
* @throws TwitchException |
327
|
|
|
*/ |
328
|
|
|
public function streamGet($channel) |
329
|
|
|
{ |
330
|
|
|
$stream = new Methods\Stream($this->request); |
331
|
|
|
|
332
|
|
|
return $stream->getStream($channel); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Search live streams |
337
|
|
|
* @param string $query |
338
|
|
|
* @param null $limit |
339
|
|
|
* @param null $offset |
340
|
|
|
* @param null $hls |
341
|
|
|
* @return \stdClass |
342
|
|
|
* @throws TwitchException |
343
|
|
|
*/ |
344
|
|
|
public function streamSearch($query, $limit = null, $offset = null, $hls = null) |
345
|
|
|
{ |
346
|
|
|
$queryString = $this->helper->buildQueryString(array( |
347
|
|
|
'query' => $query, |
348
|
|
|
'limit' => $limit, |
349
|
|
|
'offset' => $offset, |
350
|
|
|
'hls' => $hls, |
351
|
|
|
)); |
352
|
|
|
|
353
|
|
|
$search = new Methods\Search($this->request); |
354
|
|
|
|
355
|
|
|
return $search->streams($queryString); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Summarize streams |
360
|
|
|
* @param null $game |
361
|
|
|
* @param array|null $channels |
362
|
|
|
* @param null $hls |
363
|
|
|
* @return \stdClass |
364
|
|
|
* @throws TwitchException |
365
|
|
|
*/ |
366
|
|
View Code Duplication |
public function streamsSummarize($game = null, array $channels = null, $hls = null) |
367
|
|
|
{ |
368
|
|
|
if (!empty($channels)) { |
369
|
|
|
$channels = implode(',', $channels); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$queryString = $this->helper->buildQueryString(array( |
373
|
|
|
'game' => $game, |
374
|
|
|
'channel' => $channels, |
375
|
|
|
'hls' => $hls, |
376
|
|
|
)); |
377
|
|
|
|
378
|
|
|
$stream = new Methods\Stream($this->request); |
379
|
|
|
|
380
|
|
|
return $stream->getSummary($queryString); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Get featured streams |
385
|
|
|
* @param null $limit |
386
|
|
|
* @param null $offset |
387
|
|
|
* @param null $hls |
388
|
|
|
* @return \stdClass |
389
|
|
|
* @throws TwitchException |
390
|
|
|
*/ |
391
|
|
View Code Duplication |
public function streamsFeatured($limit = null, $offset = null, $hls = null) |
392
|
|
|
{ |
393
|
|
|
$queryString = $this->helper->buildQueryString(array( |
394
|
|
|
'limit' => $limit, |
395
|
|
|
'offset' => $offset, |
396
|
|
|
'hls' => $hls, |
397
|
|
|
)); |
398
|
|
|
|
399
|
|
|
$stream = new Methods\Stream($this->request); |
400
|
|
|
|
401
|
|
|
return $stream->getFeatured($queryString); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Get video |
406
|
|
|
* @param string $videoId |
407
|
|
|
* @return \stdClass |
408
|
|
|
* @throws TwitchException |
409
|
|
|
*/ |
410
|
|
|
public function videoGet($videoId) |
411
|
|
|
{ |
412
|
|
|
$video = new Methods\Video($this->request); |
413
|
|
|
|
414
|
|
|
return $video->getVideo($videoId); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Returns top videos |
419
|
|
|
* @param integer $limit |
420
|
|
|
* @param integer $offset |
421
|
|
|
* @param string $game |
422
|
|
|
* @param string $period |
423
|
|
|
* @return \stdClass |
424
|
|
|
* @throws TwitchException |
425
|
|
|
*/ |
426
|
|
View Code Duplication |
public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
427
|
|
|
{ |
428
|
|
|
$queryString = $this->helper->buildQueryString(array( |
429
|
|
|
'limit' => $limit, |
430
|
|
|
'offset' => $offset, |
431
|
|
|
'game' => $game, |
432
|
|
|
'period' => $period, |
433
|
|
|
)); |
434
|
|
|
|
435
|
|
|
$video = new Methods\Video($this->request); |
436
|
|
|
|
437
|
|
|
return $video->getTop($queryString); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Get videos for a channel |
442
|
|
|
* @param $channel |
443
|
|
|
* @param null $limit |
444
|
|
|
* @param null $offset |
445
|
|
|
* @param bool $broadcasts |
446
|
|
|
* @param bool $hls |
447
|
|
|
* @return \stdClass |
448
|
|
|
* @throws TwitchException |
449
|
|
|
*/ |
450
|
|
View Code Duplication |
public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
451
|
|
|
{ |
452
|
|
|
$queryString = $this->helper->buildQueryString(array( |
453
|
|
|
'limit' => $limit, |
454
|
|
|
'offset' => $offset, |
455
|
|
|
'broadcasts' => $broadcasts, |
456
|
|
|
'hls' => $hls, |
457
|
|
|
)); |
458
|
|
|
|
459
|
|
|
$video = new Methods\Video($this->request); |
460
|
|
|
|
461
|
|
|
return $video->getChannelVideos($channel, $queryString); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Returns a links object to all other chat endpoints |
466
|
|
|
* @param string $channelName |
467
|
|
|
* @return \stdClass |
468
|
|
|
* @throws TwitchException |
469
|
|
|
*/ |
470
|
|
|
public function chatGet($channelName) |
471
|
|
|
{ |
472
|
|
|
$chat = new Methods\Chat($this->request); |
473
|
|
|
|
474
|
|
|
return $chat->getChat($channelName); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Get a chat's emoticons |
479
|
|
|
* @return \stdClass |
480
|
|
|
* @throws TwitchException |
481
|
|
|
*/ |
482
|
|
|
public function chatEmoticons() |
483
|
|
|
{ |
484
|
|
|
$chat = new Methods\Chat($this->request); |
485
|
|
|
|
486
|
|
|
return $chat->getEmoticons(); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Returns a list of emoticons |
491
|
|
|
* @param string $emoteset |
492
|
|
|
* @return \stdClass |
493
|
|
|
* @throws TwitchException |
494
|
|
|
*/ |
495
|
|
View Code Duplication |
public function chatEmoticonsImages($emoteset = null) |
496
|
|
|
{ |
497
|
|
|
$queryString = $this->helper->buildQueryString(array( |
498
|
|
|
'emotesets' => $emoteset, |
499
|
|
|
)); |
500
|
|
|
|
501
|
|
|
$chat = new Methods\Chat($this->request); |
502
|
|
|
|
503
|
|
|
return $chat->getEmoticonImages($queryString); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Returns a list of chat badges |
508
|
|
|
* @param string $channelName |
509
|
|
|
* @return \stdClass |
510
|
|
|
* @throws TwitchException |
511
|
|
|
*/ |
512
|
|
|
public function chatBadges($channelName) |
513
|
|
|
{ |
514
|
|
|
$chat = new Methods\Chat($this->request); |
515
|
|
|
|
516
|
|
|
return $chat->getBadges($channelName); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Get top games |
521
|
|
|
* @param integer $limit |
522
|
|
|
* @param integer $offset |
523
|
|
|
* @return \stdClass |
524
|
|
|
* @throws TwitchException |
525
|
|
|
*/ |
526
|
|
View Code Duplication |
public function gamesTop($limit = null, $offset = null) |
527
|
|
|
{ |
528
|
|
|
$queryString = $this->helper->buildQueryString(array( |
529
|
|
|
'limit' => $limit, |
530
|
|
|
'offset' => $offset, |
531
|
|
|
)); |
532
|
|
|
|
533
|
|
|
$game = new Methods\Game($this->request); |
534
|
|
|
|
535
|
|
|
return $game->getTop($queryString); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Search games |
540
|
|
|
* @param string $query |
541
|
|
|
* @param null $type |
542
|
|
|
* @param null $live |
543
|
|
|
* @return \stdClass |
544
|
|
|
* @throws TwitchException |
545
|
|
|
*/ |
546
|
|
View Code Duplication |
public function gameSearch($query, $type = null, $live = null) |
547
|
|
|
{ |
548
|
|
|
$queryString = $this->helper->buildQueryString(array( |
549
|
|
|
'query' => $query, |
550
|
|
|
'type' => $type, |
551
|
|
|
'live' => $live, |
552
|
|
|
)); |
553
|
|
|
|
554
|
|
|
$search = new Methods\Search($this->request); |
555
|
|
|
|
556
|
|
|
return $search->games($queryString); |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Get HTML code for stream embedding |
561
|
|
|
* @param $channel |
562
|
|
|
* @param int $width |
563
|
|
|
* @param int $height |
564
|
|
|
* @param int $volume |
565
|
|
|
* @return string |
566
|
|
|
* @deprecated Will be replaced with Embed method |
567
|
|
|
*/ |
568
|
|
View Code Duplication |
public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
569
|
|
|
{ |
570
|
|
|
return '<object type="application/x-shockwave-flash" |
571
|
|
|
height="' . $height . '" |
572
|
|
|
width="' . $width . '" |
573
|
|
|
id="live_embed_player_flash" |
574
|
|
|
data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' . $channel . '" |
575
|
|
|
bgcolor="#000000"> |
576
|
|
|
<param name="allowFullScreen" |
577
|
|
|
value="true" /> |
578
|
|
|
<param name="allowScriptAccess" |
579
|
|
|
value="always" /> |
580
|
|
|
<param name="allowNetworking" |
581
|
|
|
value="all" /> |
582
|
|
|
<param name="movie" |
583
|
|
|
value="http://www.twitch.tv/widgets/live_embed_player.swf" /> |
584
|
|
|
<param name="flashvars" |
585
|
|
|
value="hostname=www.twitch.tv&channel=' . $channel . '&auto_play=true&start_volume=' . $volume . '" /> |
586
|
|
|
</object>'; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Get HTML code for video embedding |
591
|
|
|
* @param $channel |
592
|
|
|
* @param $chapterid |
593
|
|
|
* @param int $width |
594
|
|
|
* @param int $height |
595
|
|
|
* @param int $volume |
596
|
|
|
* @return string |
597
|
|
|
* @deprecated Will be replaced with Embed method |
598
|
|
|
*/ |
599
|
|
View Code Duplication |
public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
600
|
|
|
{ |
601
|
|
|
return '<object bgcolor="#000000" |
602
|
|
|
data="http://www.twitch.tv/widgets/archive_embed_player.swf" |
603
|
|
|
width="' . $width . '" |
604
|
|
|
height="' . $height . '" |
605
|
|
|
id="clip_embed_player_flash" |
606
|
|
|
type="application/x-shockwave-flash"> |
607
|
|
|
<param name="movie" |
608
|
|
|
value="http://www.twitch.tv/widgets/archive_embed_player.swf" /> |
609
|
|
|
<param name="allowScriptAccess" |
610
|
|
|
value="always" /> |
611
|
|
|
<param name="allowNetworking" |
612
|
|
|
value="all" /> |
613
|
|
|
<param name="allowFullScreen" |
614
|
|
|
value="true" /> |
615
|
|
|
<param name="flashvars" |
616
|
|
|
value="channel=' . $channel . '&start_volume=' . $volume . '&auto_play=false&chapter_id=' . $chapterid . '" /> |
617
|
|
|
</object>'; |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* Get HTML code for chat embedding |
622
|
|
|
* @param $channel |
623
|
|
|
* @param int $width |
624
|
|
|
* @param int $height |
625
|
|
|
* @return string |
626
|
|
|
* @deprecated Will be replaced with Embed method |
627
|
|
|
*/ |
628
|
|
|
public function embedChat($channel, $width = 400, $height = 300) |
629
|
|
|
{ |
630
|
|
|
return '<iframe frameborder="0" |
631
|
|
|
scrolling="no" |
632
|
|
|
id="chat_embed" |
633
|
|
|
src="http://twitch.tv/chat/embed?channel=' . $channel . '&popout_chat=true" |
634
|
|
|
height="' . $height . '" |
635
|
|
|
width="' . $width . '"> |
636
|
|
|
</iframe>'; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* Get login URL for authentication |
641
|
|
|
* @param string $scope Specify which permissions your app requires (space separated list) |
642
|
|
|
* @return \stdClass |
643
|
|
|
* @throws TwitchException |
644
|
|
|
*/ |
645
|
|
View Code Duplication |
public function authLoginURL($scope) |
646
|
|
|
{ |
647
|
|
|
$this->checkConfig(); |
648
|
|
|
|
649
|
|
|
$queryString = $this->helper->buildQueryString(array( |
650
|
|
|
'response_type' => 'code', |
651
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
652
|
|
|
'redirect_uri' => $this->getConfigParam('redirect_uri'), |
653
|
|
|
'scope' => $scope, |
654
|
|
|
)); |
655
|
|
|
|
656
|
|
|
$auth = new Methods\Auth($this->request); |
657
|
|
|
|
658
|
|
|
return $auth->getLoginURL($queryString); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* Get authentication access token |
663
|
|
|
* @param string $code returned after app authorization by user |
664
|
|
|
* @return \stdClass |
665
|
|
|
* @throws TwitchException |
666
|
|
|
*/ |
667
|
|
View Code Duplication |
public function authAccessTokenGet($code) |
668
|
|
|
{ |
669
|
|
|
$this->checkConfig(); |
670
|
|
|
|
671
|
|
|
$queryString = $this->helper->buildQueryString(array( |
672
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
673
|
|
|
'client_secret' => $this->getConfigParam('client_secret'), |
674
|
|
|
'grant_type' => 'authorization_code', |
675
|
|
|
'redirect_uri' => $this->getConfigParam('redirect_uri'), |
676
|
|
|
'code' => $code, |
677
|
|
|
)); |
678
|
|
|
|
679
|
|
|
$auth = new Methods\Auth($this->request); |
680
|
|
|
|
681
|
|
|
return $auth->getAccessToken($queryString); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Get the authenticated user |
686
|
|
|
* - requires scope 'user_read' |
687
|
|
|
* @param string |
688
|
|
|
* @return \stdClass |
689
|
|
|
* @throws TwitchException |
690
|
|
|
*/ |
691
|
|
|
public function authUserGet($token) |
692
|
|
|
{ |
693
|
|
|
$this->checkConfig(); |
694
|
|
|
$queryString = $this->getAuthString($token); |
695
|
|
|
$user = new Methods\User($this->request); |
696
|
|
|
|
697
|
|
|
return $user->getUserAuth($queryString); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Returns a list of blocks |
702
|
|
|
* - required scope 'user_blocks_read' |
703
|
|
|
* @param string $token |
704
|
|
|
* @param string $user |
705
|
|
|
* @param integer $limit |
706
|
|
|
* @param integer $offset |
707
|
|
|
* @return \stdClass |
708
|
|
|
* @throws TwitchException |
709
|
|
|
*/ |
710
|
|
|
public function authUserBlocks($token, $user, $limit = null, $offset = null) |
711
|
|
|
{ |
712
|
|
|
$this->checkConfig(); |
713
|
|
|
|
714
|
|
|
$queryString = $this->helper->buildQueryString(array( |
715
|
|
|
'oauth_token' => $token, |
716
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
717
|
|
|
'limit' => $limit, |
718
|
|
|
'offset' => $offset, |
719
|
|
|
)); |
720
|
|
|
|
721
|
|
|
$block = new Methods\Block($this->request); |
722
|
|
|
|
723
|
|
|
return $block->getBlocks($user, $queryString); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Adds $target to $user block list |
728
|
|
|
* - requires scope 'user_blocks_edit' |
729
|
|
|
* @param string $token |
730
|
|
|
* @param string $user |
731
|
|
|
* @param string $target |
732
|
|
|
* @return \stdClass |
733
|
|
|
* @throws TwitchException |
734
|
|
|
*/ |
735
|
|
View Code Duplication |
public function authBlockTarget($token, $user, $target) |
736
|
|
|
{ |
737
|
|
|
$this->checkConfig(); |
738
|
|
|
$queryString = $this->getAuthString($token); |
739
|
|
|
$block = new Methods\Block($this->request); |
740
|
|
|
|
741
|
|
|
return $block->blockTarget($user, $target, $queryString); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* Removes $target from $user block list |
746
|
|
|
* - requires scope 'user_blocks_edit' |
747
|
|
|
* @param string $token |
748
|
|
|
* @param string $user |
749
|
|
|
* @param string $target |
750
|
|
|
* @return \stdClass |
751
|
|
|
* @throws TwitchException |
752
|
|
|
*/ |
753
|
|
View Code Duplication |
public function authRemoveTarget($token, $user, $target) |
754
|
|
|
{ |
755
|
|
|
$this->checkConfig(); |
756
|
|
|
$queryString = $this->getAuthString($token); |
757
|
|
|
$block = new Methods\Block($this->request); |
758
|
|
|
|
759
|
|
|
return $block->removeTarget($user, $target, $queryString); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
/** |
763
|
|
|
* Get the authenticated channel |
764
|
|
|
* - requires scope 'channel_read' |
765
|
|
|
* @param string |
766
|
|
|
* @return \stdClass |
767
|
|
|
* @throws TwitchException |
768
|
|
|
*/ |
769
|
|
View Code Duplication |
public function authChannelGet($token) |
770
|
|
|
{ |
771
|
|
|
$this->checkConfig(); |
772
|
|
|
$queryString = $this->getAuthString($token); |
773
|
|
|
$channels = new Methods\Channel($this->request); |
774
|
|
|
|
775
|
|
|
return $channels->getChannel($queryString); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Update channel's status or game |
780
|
|
|
* - requires scope 'channel_editor' |
781
|
|
|
* @param $token |
782
|
|
|
* @param string $channelName |
783
|
|
|
* @param string $status |
784
|
|
|
* @param string $game |
785
|
|
|
* @param integer $delay |
786
|
|
|
* @return \stdClass |
787
|
|
|
* @throws TwitchException |
788
|
|
|
*/ |
789
|
|
|
public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
790
|
|
|
{ |
791
|
|
|
$this->checkConfig(); |
792
|
|
|
$queryString = $this->getAuthString($token); |
793
|
|
|
|
794
|
|
|
$data = $this->helper->buildQueryString(array( |
795
|
|
|
'channel[status]' => $status, |
796
|
|
|
'channel[game]' => $game, |
797
|
|
|
'channel[delay]' => $delay, |
798
|
|
|
)); |
799
|
|
|
|
800
|
|
|
$channel = new Methods\Channel($this->request); |
801
|
|
|
|
802
|
|
|
return $channel->setChannel($channelName, $queryString, $data); |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* Resets channel's stream key |
807
|
|
|
* - requires scope 'channel_stream' |
808
|
|
|
* @param string $token |
809
|
|
|
* @param string $channelName |
810
|
|
|
* @return \stdClass |
811
|
|
|
* @throws TwitchException |
812
|
|
|
*/ |
813
|
|
View Code Duplication |
public function authChannelResetKey($token, $channelName) |
814
|
|
|
{ |
815
|
|
|
$this->checkConfig(); |
816
|
|
|
$queryString = $this->getAuthString($token); |
817
|
|
|
$channel = new Methods\Channel($this->request); |
818
|
|
|
|
819
|
|
|
return $channel->resetStreamKey($channelName, $queryString); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* Returns an array of users who are editors of specified channel |
824
|
|
|
* - requires scope 'channel_read' |
825
|
|
|
* @param string |
826
|
|
|
* @param string |
827
|
|
|
* @return \stdClass |
828
|
|
|
* @throws TwitchException |
829
|
|
|
*/ |
830
|
|
View Code Duplication |
public function authChannelEditors($token, $channel) |
831
|
|
|
{ |
832
|
|
|
$this->checkConfig(); |
833
|
|
|
$queryString = $this->getAuthString($token); |
834
|
|
|
$channels = new Methods\Channel($this->request); |
835
|
|
|
|
836
|
|
|
return $channels->getEditors($channel, $queryString); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
/** |
840
|
|
|
* Returns an array of subscriptions who are subscribed to specified channel |
841
|
|
|
* - requires scope 'channel_subscriptions' |
842
|
|
|
* @param string $token - user's access token |
843
|
|
|
* @param string $channel |
844
|
|
|
* @param integer $limit - can be up to 100 |
845
|
|
|
* @param integer $offset |
846
|
|
|
* @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
847
|
|
|
* @return \stdClass |
848
|
|
|
* @throws TwitchException |
849
|
|
|
*/ |
850
|
|
View Code Duplication |
public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
851
|
|
|
{ |
852
|
|
|
$this->checkConfig(); |
853
|
|
|
|
854
|
|
|
$queryString = $this->helper->buildQueryString(array( |
855
|
|
|
'oauth_token' => $token, |
856
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
857
|
|
|
'direction' => $direction, |
858
|
|
|
'limit' => $limit, |
859
|
|
|
'offset' => $offset |
860
|
|
|
)); |
861
|
|
|
|
862
|
|
|
$subscription = new Methods\Subscription($this->request); |
863
|
|
|
|
864
|
|
|
return $subscription->getSubscriptions($channel, $queryString); |
865
|
|
|
} |
866
|
|
|
|
867
|
|
|
/** |
868
|
|
|
* Returns user object if that user is subscribed |
869
|
|
|
* - requires scope 'channel_check_subscription' for channel |
870
|
|
|
* @param string $token |
871
|
|
|
* @param string $channel |
872
|
|
|
* @param string $user |
873
|
|
|
* @return \stdClass |
874
|
|
|
* @throws TwitchException |
875
|
|
|
*/ |
876
|
|
View Code Duplication |
public function authSubscribedUser($token, $channel, $user) |
877
|
|
|
{ |
878
|
|
|
$this->checkConfig(); |
879
|
|
|
$queryString = $this->getAuthString($token); |
880
|
|
|
$subscription = new Methods\Subscription($this->request); |
881
|
|
|
|
882
|
|
|
return $subscription->getSubscribedUser($channel, $user, $queryString); |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
/** |
886
|
|
|
* Returns a channel object that user subscribes to |
887
|
|
|
* - requires scope 'user_subscriptions' for user |
888
|
|
|
* @param string $token |
889
|
|
|
* @param string $user |
890
|
|
|
* @param string $channel |
891
|
|
|
* @return \stdClass |
892
|
|
|
* @throws TwitchException |
893
|
|
|
*/ |
894
|
|
View Code Duplication |
public function authSubscribedToChannel($token, $user, $channel) |
895
|
|
|
{ |
896
|
|
|
$this->checkConfig(); |
897
|
|
|
$queryString = $this->getAuthString($token); |
898
|
|
|
$subscription = new Methods\Subscription($this->request); |
899
|
|
|
|
900
|
|
|
return $subscription->getSubscribedToChannel($user, $channel, $queryString); |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
/** |
904
|
|
|
* List the live streams that the authenticated user is following |
905
|
|
|
* - requires scope 'user_read' |
906
|
|
|
* @param string |
907
|
|
|
* @param integer $limit |
908
|
|
|
* @param integer $offset |
909
|
|
|
* @param bool $hls |
910
|
|
|
* @return \stdClass |
911
|
|
|
* @throws TwitchException |
912
|
|
|
*/ |
913
|
|
View Code Duplication |
public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
914
|
|
|
{ |
915
|
|
|
$this->checkConfig(); |
916
|
|
|
|
917
|
|
|
$queryString = $this->helper->buildQueryString(array( |
918
|
|
|
'oauth_token' => $token, |
919
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
920
|
|
|
'limit' => $limit, |
921
|
|
|
'offset' => $offset, |
922
|
|
|
'hls' => $hls, |
923
|
|
|
)); |
924
|
|
|
|
925
|
|
|
$user = new Methods\User($this->request); |
926
|
|
|
|
927
|
|
|
return $user->getFollowedStreams($queryString); |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
/** |
931
|
|
|
* Get streams helper |
932
|
|
|
* @param null $game |
933
|
|
|
* @param string|null $channels |
934
|
|
|
* @param null $limit |
935
|
|
|
* @param null $offset |
936
|
|
|
* @param null $embeddable |
937
|
|
|
* @param null $hls |
938
|
|
|
* @return \stdClass |
939
|
|
|
* @throws TwitchException |
940
|
|
|
*/ |
941
|
|
|
public function getStreams($game = null, $channels = null, $limit = null, $offset = null, $embeddable = null, $hls = null) |
942
|
|
|
{ |
943
|
|
|
$params = array( |
944
|
|
|
'game' => $game, |
945
|
|
|
'channel' => !empty($channels) ? $channels : null, |
946
|
|
|
'limit' => $limit, |
947
|
|
|
'offset' => $offset, |
948
|
|
|
'embeddable' => $embeddable, |
949
|
|
|
'hls' => $hls, |
950
|
|
|
); |
951
|
|
|
|
952
|
|
|
$queryString = $this->helper->buildQueryString($params); |
953
|
|
|
$stream = new Methods\Stream($this->request); |
954
|
|
|
|
955
|
|
|
return $stream->getStreams($queryString); |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
/** |
959
|
|
|
* Validate parameters for authentication |
960
|
|
|
* @param array |
961
|
|
|
* @return boolean |
962
|
|
|
*/ |
963
|
|
|
private function configValidate($config) |
964
|
|
|
{ |
965
|
|
|
$check = array('client_id', 'client_secret', 'redirect_uri'); |
966
|
|
|
|
967
|
|
|
foreach ($check AS $val) { |
968
|
|
|
if (!array_key_exists($val, $config) || |
969
|
|
|
(empty($config[$val]) || |
970
|
|
|
!is_string($config[$val])) |
971
|
|
|
) { |
972
|
|
|
return false; |
973
|
|
|
} |
974
|
|
|
} |
975
|
|
|
|
976
|
|
|
return true; |
977
|
|
|
} |
978
|
|
|
|
979
|
|
|
/** |
980
|
|
|
* Returns string for auth |
981
|
|
|
* @param string $token |
982
|
|
|
* @return null|string |
983
|
|
|
* @throws TwitchException |
984
|
|
|
*/ |
985
|
|
|
private function getAuthString($token) |
986
|
|
|
{ |
987
|
|
|
return $this->helper->buildQueryString(array( |
988
|
|
|
'oauth_token' => $token, |
989
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
990
|
|
|
)); |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
/** |
994
|
|
|
* Configuration exception |
995
|
|
|
* @throws TwitchException |
996
|
|
|
*/ |
997
|
|
|
private function configException() |
998
|
|
|
{ |
999
|
|
|
throw new TwitchException('Cannot call authenticate functions without valid API configuration'); |
1000
|
|
|
} |
1001
|
|
|
} |
1002
|
|
|
|