Completed
Push — master ( 5f8f61...473df9 )
by Josef
03:42 queued 01:55
created

TwitchSDK::chatGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ritero\SDK\TwitchTV;
4
5
/**
6
 * TwitchTV API SDK for PHP
7
 *
8
 * PHP SDK for interacting with the TwitchTV API
9
 *
10
 * @author Josef Ohnheiser <[email protected]>
11
 * @license https://github.com/jofner/Twitch-SDK/blob/master/LICENSE.md MIT
12
 * @homepage https://github.com/jofner/Twitch-SDK
13
 * @version 1.0.0
14
 */
15
class TwitchSDK
16
{
17
    /** @var array */
18
    protected $auth_config = false;
19
20
    /** @var integer Set timeout default. */
21
    public $timeout = 30;
22
23
    /** @var integer Set connect timeout */
24
    public $connect_timeout = 30;
25
26
    /** @var boolean Verify SSL Cert */
27
    public $ssl_verifypeer = false;
28
29
    /** @var integer Contains the last HTTP status code returned */
30
    public $http_code = 0;
31
32
    /** @var array Contains the last Server headers returned */
33
    public $http_header = array();
34
35
    /** @var array Contains the last HTTP headers returned */
36
    public $http_info = array();
37
38
    /** @var boolean Throw cURL errors */
39
    public $throw_curl_errors = true;
40
41
    /** @var string Set the useragnet */
42
    private $useragent = 'ritero TwitchSDK dev-0.4.*';
43
44
    /** @var string */
45
    private $clientId;
46
47
    /**
48
     * TwitchAPI URI's
49
     */
50
    const URL_TWITCH = 'https://api.twitch.tv/kraken/';
51
    const URI_USER = 'users/';
52
    const URI_USER_FOLLOWS_CHANNEL = '/users/%s/follows/channels';
53
    const URI_USER_FOLLOW_RELATION = '/users/%s/follows/channels/%s';
54
    const URI_CHANNEL = 'channels/';
55
    const URI_CHANNEL_FOLLOWS = 'channels/%s/follows';
56
    const URI_CHANNEL_SUBSCRIPTIONS = 'channels/%s/subscriptions';
57
    const URI_STREAM = 'streams/';
58
    const URI_STREAM_SUMMARY = 'streams/summary/';
59
    const URI_STREAMS_FEATURED = 'streams/featured/';
60
    const URI_STREAMS_SEARCH = 'search/streams/';
61
    const URI_VIDEO = 'videos/';
62
    const URI_CHAT = 'chat/';
63
    const URI_CHAT_EMOTICONS = 'chat/emoticons';
64
    const URI_GAMES_TOP = 'games/top/';
65
    const URI_AUTH = 'oauth2/authorize';
66
    const URI_AUTH_TOKEN = 'oauth2/token';
67
    const URI_USER_AUTH = 'user';
68
    const URI_CHANNEL_AUTH = 'channel';
69
    const URI_CHANNEL_EDITORS_AUTH = 'channels/%s/editors';
70
    const URI_STREAMS_FOLLOWED_AUTH = 'streams/followed';
71
    const URI_TEAMS = 'teams/';
72
73
    /**
74
     * For teams API we have different URI's and
75
     * use HTTP instead of HTTPS
76
     */
77
    const URL_TWITCH_TEAM = "http://api.twitch.tv/api/team/";
78
79
    /**
80
     * @return string
81
     */
82
    public function getClientId()
83
    {
84
        return $this->clientId;
85
    }
86
87
    /**
88
     * @param string $clientId
89
     */
90
    public function setClientId($clientId)
91
    {
92
        $this->clientId = $clientId;
93
    }
94
95
    /**
96
     * SDK constructor
97
     * @param   array
98
     * @throws  \ritero\SDK\TwitchTV\TwitchException
99
     * @throws \InvalidArgumentException
100
     */
101
    public function __construct($config = array())
102
    {
103
        if (!in_array('curl', get_loaded_extensions())) {
104
            throw new TwitchException('cURL extension is not installed and is required');
105
        }
106
107
        if (!array_key_exists('clientId', $config)) {
108
            throw new \InvalidArgumentException('Missing required Client-ID parameter in config
109
                @see https://blog.twitch.tv/client-id-required-for-kraken-api-calls-afbb8e95f843');
110
        }
111
112
        if (!empty($config)) {
113
            if ($this->configValidate($config) === true) {
114
                $this->auth_config = $config;
115
                $this->setClientId($config['clientId']);
116
            } else {
117
                throw new TwitchException('Wrong Twitch API config parameters');
118
            }
119
        }
120
    }
121
122
    /**
123
     * Basic information about the API and authentication status
124
     * @param   string
125
     * @return  stdClass
126
     */
127
    public function status($token = null)
128
    {
129
        $auth = null;
130
131
        if (!is_null($token)) {
132
            if ($this->auth_config === false) {
133
                $this->authConfigException();
134
            } else {
135
                $auth = $this->buildQueryString(array('oauth_token' => $token));
136
            }
137
        }
138
139
        return $this->request($auth);
140
    }
141
142
    /**
143
     * Get the specified user
144
     * @param   string
145
     * @return  stdClass
146
     */
147
    public function userGet($username)
148
    {
149
        return $this->request(self::URI_USER . $username);
150
    }
151
152
    /**
153
     * Get a user's list of followed channels
154
     * @param   integer
155
     * @param   integer
156
     * @param   integer
157
     * @return  stdClass
158
     */
159 View Code Duplication
    public function userFollowChannels($user, $limit = null, $offset = null)
160
    {
161
        $query_string = $this->buildQueryString(array(
162
            'limit' => $limit,
163
            'offset' => $offset,
164
        ));
165
166
        return $this->request(sprintf(self::URI_USER_FOLLOWS_CHANNEL, $user) . $query_string);
167
    }
168
169
    /**
170
     * Get the status of a follow relationship
171
     * @param   string
172
     * @param   string
173
     * @return  stdClass
174
     */
175
    public function userFollowRelationship($user, $channel)
176
    {
177
        return $this->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel));
178
    }
179
180
    /**
181
     * Set user to follow given channel
182
     * @param   string
183
     * @param   string
184
     * @param   string
185
     * @return  stdClass
186
     */
187 View Code Duplication
    public function userFollowChannel($user, $channel, $userToken)
188
    {
189
        $query_string = $this->buildQueryString(array(
190
            'oauth_token' => $userToken,
191
        ));
192
193
        return $this->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel) . $query_string, 'PUT');
194
    }
195
196
    /**
197
     * Set user to unfollow given channel
198
     * @param   string
199
     * @param   string
200
     * @param   string
201
     * @return  stdClass
202
     */
203 View Code Duplication
    public function userUnfollowChannel($user, $channel, $userToken)
204
    {
205
        $query_string = $this->buildQueryString(array(
206
            'oauth_token' => $userToken,
207
        ));
208
209
        return $this->request(sprintf(self::URI_USER_FOLLOW_RELATION, $user, $channel) . $query_string, 'DELETE');
210
    }
211
212
    /**
213
     * Get the specified channel
214
     * @param   string
215
     * @return  stdClass
216
     */
217
    public function channelGet($channel)
218
    {
219
        return $this->request(self::URI_CHANNEL . $channel);
220
    }
221
222
    /**
223
     * Get the specified team
224
     * @param   string
225
     * @return  stdClass
226
     */
227
    public function teamGet($teamName)
228
    {
229
        return $this->request(self::URI_TEAMS . $teamName);
230
    }
231
232
    /**
233
     *
234
     */
235
    public function teamMembersAll($teamName)
236
    {
237
        return $this->teamRequest($teamName . '/all_channels')->channels;
238
    }
239
240
    /**
241
     * Returns an array of users who follow the specified channel
242
     * @param   string
243
     * @param   integer
244
     * @param   integer
245
     * @return  stdClass
246
     */
247 View Code Duplication
    public function channelFollows($channel, $limit = null, $offset = null)
248
    {
249
        $query_string = $this->buildQueryString(array(
250
            'limit' => $limit,
251
            'offset' => $offset,
252
        ));
253
254
        return $this->request(sprintf(self::URI_CHANNEL_FOLLOWS, $channel) . $query_string);
255
    }
256
257
    /**
258
     * Get the specified channel's stream
259
     * @param   string
260
     * @return  stdClass
261
     */
262
    public function streamGet($channel)
263
    {
264
        return $this->request(self::URI_STREAM . $channel);
265
    }
266
267
    /**
268
     * Search live streams
269
     * @param   string
270
     * @param   integer
271
     * @param   integer
272
     * @return  stdClass
273
     */
274 View Code Duplication
    public function streamSearch($query, $limit = null, $offset = null)
275
    {
276
        $query_string = $this->buildQueryString(array(
277
            'query' => $query,
278
            'limit' => $limit,
279
            'offset' => $offset,
280
        ));
281
282
        return $this->request(self::URI_STREAMS_SEARCH . $query_string);
283
    }
284
285
    /**
286
     * Summarize streams
287
     * @param   string
288
     * @param   array
289
     * @param   boolean
290
     * @return  stdClass
291
     */
292
    public function streamsSummarize($game = null, array $channels = null, $hls = null)
293
    {
294
        if (!empty($channels)) {
295
            $channels = implode(',', $channels);
296
        }
297
298
        $query_string = $this->buildQueryString(array(
299
            'game' => $game,
300
            'channel' => $channels,
301
            'hls' => $hls,
302
        ));
303
304
        return $this->request(self::URI_STREAM_SUMMARY . $query_string);
305
    }
306
307
    /**
308
     * Get featured streams
309
     * @param   integer
310
     * @param   integer
311
     * @param   boolean
312
     * @return  stdClass
313
     */
314 View Code Duplication
    public function streamsFeatured($limit = null, $offset = null, $hls = null)
315
    {
316
        $query_string = $this->buildQueryString(array(
317
            'limit' => $limit,
318
            'offset' => $offset,
319
            'hls' => $hls,
320
        ));
321
322
        return $this->request(self::URI_STREAMS_FEATURED . $query_string);
323
    }
324
325
    /**
326
     * Get streams by channel
327
     * @param   array
328
     * @param   integer
329
     * @param   integer
330
     * @param   boolean
331
     * @param   boolean
332
     * @return  stdClass
333
     */
334
    public function streamsByChannels($channels, $limit = null, $offset = null, $embeddable = null, $hls = null)
335
    {
336
        $channels_string = implode(',', $channels);
337
338
        return $this->getStreams(null, $limit, $offset, $channels_string, $embeddable, $hls);
339
    }
340
341
    /**
342
     * Get streams by game
343
     * @param   string
344
     * @param   integer
345
     * @param   integer
346
     * @param   boolean
347
     * @param   boolean
348
     * @return  stdClass
349
     */
350
    public function streamsByGame($game, $limit = null, $offset = null, $embeddable = null, $hls = null)
351
    {
352
        return $this->getStreams($game, $limit, $offset, null, $embeddable, $hls);
353
    }
354
355
    /**
356
     * Get video
357
     * @param   integer
358
     * @return  stdClass
359
     */
360
    public function videoGet($video)
361
    {
362
        return $this->request(self::URI_VIDEO . $video);
363
    }
364
365
    /**
366
     * Get videos for a channel
367
     * @param   string
368
     * @param   integer
369
     * @param   integer
370
     * @return  stdClass
371
     */
372
    public function videosByChannel($channel, $limit = null, $offset = null)
373
    {
374
        $query_string = $this->buildQueryString(array(
375
            'limit' => $limit,
376
            'offset' => $offset,
377
        ));
378
379
        return $this->request(self::URI_CHANNEL . $channel . '/' . self::URI_VIDEO . $query_string);
380
    }
381
382
    /**
383
     * Get the specified channel's chat
384
     * @param   string
385
     * @return  stdClass
386
     */
387
    public function chatGet($channel)
388
    {
389
        return $this->request(self::URI_CHAT . $channel);
390
    }
391
392
    /**
393
     * Get a chat's emoticons
394
     * @return  stdClass
395
     */
396
    public function chatEmoticons()
397
    {
398
        return $this->request(self::URI_CHAT_EMOTICONS);
399
    }
400
401
    /**
402
     * Get top games
403
     * @param   integer
404
     * @param   integer
405
     * @return  stdClass
406
     */
407 View Code Duplication
    public function gamesTop($limit = null, $offset = null)
408
    {
409
        $query_string = $this->buildQueryString(array(
410
            'limit' => $limit,
411
            'offset' => $offset,
412
        ));
413
414
        return $this->request(self::URI_GAMES_TOP . $query_string);
415
    }
416
417
    /**
418
     * Get HTML code for stream embedding
419
     * @param   string
420
     * @param   integer
421
     * @param   integer
422
     * @param   integer
423
     * @return  string
424
     */
425 View Code Duplication
    public function embedStream($channel, $width = 620, $height = 378, $volume = 25)
426
    {
427
        return '<object type="application/x-shockwave-flash"
428
                height="' . $height . '"
429
                width="' . $width . '"
430
                id="live_embed_player_flash"
431
                data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' . $channel . '"
432
                bgcolor="#000000">
433
                <param  name="allowFullScreen"
434
                    value="true" />
435
                <param  name="allowScriptAccess"
436
                    value="always" />
437
                <param  name="allowNetworking"
438
                    value="all" />
439
                <param  name="movie"
440
                    value="http://www.twitch.tv/widgets/live_embed_player.swf" />
441
                <param  name="flashvars"
442
                    value="hostname=www.twitch.tv&channel=' . $channel . '&auto_play=true&start_volume=' . $volume . '" />
443
                </object>';
444
    }
445
446
    /**
447
     * Get HTML code for video embedding
448
     * @param   string
449
     * @param   integer
450
     * @param   integer
451
     * @param   integer
452
     * @param   integer
453
     * @return  string
454
     */
455 View Code Duplication
    public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25)
456
    {
457
        return '<object bgcolor="#000000"
458
                    data="http://www.twitch.tv/widgets/archive_embed_player.swf"
459
                    width="' . $width . '"
460
                    height="' . $height . '"
461
                    id="clip_embed_player_flash"
462
                    type="application/x-shockwave-flash">
463
                <param  name="movie"
464
                    value="http://www.twitch.tv/widgets/archive_embed_player.swf" />
465
                <param  name="allowScriptAccess"
466
                    value="always" />
467
                <param  name="allowNetworking"
468
                    value="all" />
469
                <param name="allowFullScreen"
470
                    value="true" />
471
                <param  name="flashvars"
472
                    value="channel=' . $channel . '&start_volume=' . $volume . '&auto_play=false&chapter_id=' . $chapterid . '" />
473
                </object>';
474
    }
475
476
    /**
477
     * Get HTML code for chat embedding
478
     * @param   string
479
     * @param   integer
480
     * @param   integer
481
     * @return  string
482
     */
483
    public function embedChat($channel, $width = 400, $height = 300)
484
    {
485
        return '<iframe frameborder="0"
486
                    scrolling="no"
487
                    id="chat_embed"
488
                    src="http://twitch.tv/chat/embed?channel=' . $channel . '&amp;popout_chat=true"
489
                    height="' . $height . '"
490
                    width="' . $width . '">
491
                </iframe>';
492
    }
493
494
    /**
495
     * Get login URL for authentication
496
     * @param   string $scope Specify which permissions your app requires (space separated list)
497
     * @return  string
498
     */
499
    public function authLoginURL($scope)
500
    {
501
        if ($this->auth_config === false) {
502
            $this->authConfigException();
503
        }
504
505
        $query_string = $this->buildQueryString(array(
506
            'response_type' => 'code',
507
            'client_id' => $this->auth_config['client_id'],
508
            'redirect_uri' => $this->auth_config['redirect_uri'],
509
            'scope' => $scope,
510
        ));
511
512
        return self::URL_TWITCH . self::URI_AUTH . $query_string;
513
    }
514
515
    /**
516
     * Get authentication access token
517
     * @param   string code returned after app authorization by user
518
     * @return  stdClass
519
     */
520
    public function authAccessTokenGet($code)
521
    {
522
        if ($this->auth_config === false) {
523
            $this->authConfigException();
524
        }
525
526
        $query_string = $this->buildQueryString(array(
527
            'client_id' => $this->auth_config['client_id'],
528
            'client_secret' => $this->auth_config['client_secret'],
529
            'grant_type' => 'authorization_code',
530
            'redirect_uri' => $this->auth_config['redirect_uri'],
531
            'code' => $code,
532
        ));
533
534
        return $this->request(self::URI_AUTH_TOKEN, 'POST', $query_string);
535
    }
536
537
    /**
538
     * Get the authenticated user
539
     *  - requires scope 'user_read'
540
     * @param   string
541
     * @return  stdClass
542
     */
543 View Code Duplication
    public function authUserGet($token)
544
    {
545
        if ($this->auth_config === false) {
546
            $this->authConfigException();
547
        }
548
549
        $query_string = $this->buildQueryString(array(
550
            'oauth_token' => $token,
551
            'client_id' => $this->auth_config['client_id'],
552
        ));
553
554
        return $this->request(self::URI_USER_AUTH . $query_string);
555
    }
556
557
    /**
558
     * Get the authenticated channel
559
     *  - requires scope 'channel_read'
560
     * @param   string
561
     * @return  stdClass
562
     */
563 View Code Duplication
    public function authChannelGet($token)
564
    {
565
        if ($this->auth_config === false) {
566
            $this->authConfigException();
567
        }
568
569
        $query_string = $this->buildQueryString(array(
570
            'oauth_token' => $token,
571
            'client_id' => $this->auth_config['client_id'],
572
        ));
573
574
        return $this->request(self::URI_CHANNEL_AUTH . $query_string);
575
    }
576
577
    /**
578
     * Returns an array of users who are editors of specified channel
579
     *  - requires scope 'channel_read'
580
     * @param   string
581
     * @param   string
582
     * @return  stdClass
583
     */
584 View Code Duplication
    public function authChannelEditors($token, $channel)
585
    {
586
        if ($this->auth_config === false) {
587
            $this->authConfigException();
588
        }
589
590
        $query_string = $this->buildQueryString(array(
591
            'oauth_token' => $token,
592
            'client_id' => $this->auth_config['client_id'],
593
        ));
594
595
        return $this->request(sprintf(self::URI_CHANNEL_EDITORS_AUTH, $channel) . $query_string);
596
    }
597
598
    /**
599
     * @description Returns an array of subscriptions who are subscribed to specified channel
600
     *  - requires scope 'channel_subscriptions'
601
     * @param   string $token - user's access token
602
     * @param   string $channel
603
     * @param   integer $limit - can be up to 100
604
     * @param   integer $offset
605
     * @param   string $direction can be DESC|ASC, if DESC - lasts will be showed first
606
     * @return  stdClass
607
     */
608
    public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC')
609
    {
610
        if ($this->auth_config === false) {
611
            $this->authConfigException();
612
        }
613
614
        $query_string = $this->buildQueryString(array(
615
            'oauth_token' => $token,
616
            'client_id' => $this->auth_config['client_id'],
617
            'direction' => $direction,
618
            'limit' => $limit,
619
            'offset' => $offset
620
        ));
621
622
        return $this->request(sprintf(self::URI_CHANNEL_SUBSCRIPTIONS, $channel) . $query_string);
623
    }
624
625
    /**
626
     * List the live streams that the authenticated user is following
627
     *  - requires scope 'user_read'
628
     *
629
     * @param string  $token  API access token of an authenticated user
630
     * @param integer $limit  Maximum number of objects in streams array
631
     *                        Default => 25
632
     *                        Maximum => 100
633
     * @param integer $offset Object offset for pagination
634
     *                        Default => 0
635
     * @param boolean $hls    Limit the results to only the streams using HLS
636
     *
637
     * @return stdClass
638
     */
639
    public function authStreamsFollowed($token, $limit = null, $offset = null, $hls = null)
640
    {
641
        if ($this->auth_config === false) {
642
            $this->authConfigException();
643
        }
644
645
        $query = array(
646
            'oauth_token' => $token,
647
            'client_id' => $this->auth_config['client_id']
648
        );
649
650
        foreach (array('limit', 'offset', 'hls') as $param) {
651
            if (!is_null($$param)) {
652
                $query[$param] = $$param;
653
            }
654
        }
655
656
        $query_string = $this->buildQueryString($query);
657
658
        return $this->request(self::URI_STREAMS_FOLLOWED_AUTH . $query_string);
659
    }
660
661
    /**
662
     * Get streams helper
663
     * @param   string
664
     * @param   integer
665
     * @param   integer
666
     * @param   string
667
     * @param   boolean
668
     * @param   boolean
669
     * @return  stdClass
670
     */
671
    public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null)
672
    {
673
        $params = array(
674
            'game' => $game,
675
            'limit' => $limit,
676
            'offset' => $offset,
677
            'channel' => !empty($channels) ? $channels : null,
678
            'embeddable' => $embeddable,
679
            'hls' => $hls,
680
        );
681
682
        $query_string = $this->buildQueryString($params);
683
684
        return $this->request(self::URI_STREAM . $query_string);
685
    }
686
687
    /**
688
     * Validate parameters for authentication
689
     * @param   array
690
     * @return  boolean
691
     */
692
    private function configValidate($config)
693
    {
694
        $check = array('client_id', 'client_secret', 'redirect_uri');
695
696
        foreach ($check AS $val) {
697
            if (!array_key_exists($val, $config) ||
698
                (empty($config[$val]) ||
699
                    !is_string($config[$val]))
700
            ) {
701
                return false;
702
            }
703
        }
704
705
        return true;
706
    }
707
708
    /**
709
     * Build query string
710
     * @param   array
711
     * @return  string
712
     */
713
    private function buildQueryString($params)
714
    {
715
        $param = array();
716
        $query_string = null;
717
718
        foreach ($params as $key => $value) {
719
            if (!empty($value)) {
720
                $param[$key] = $value;
721
            }
722
        }
723
724
        if (!empty($param)) {
725
            $query_string = '?' . http_build_query($param);
726
        }
727
728
        return $query_string;
729
    }
730
731
    /**
732
     * TwitchAPI request
733
     * @param   string
734
     * @param   string
735
     * @param   string
736
     * @return  stdClass
737
     * @throws  \ritero\SDK\TwitchTV\TwitchException
738
     */
739
    private function request($uri, $method = 'GET', $postfields = null)
740
    {
741
        $params = ['CURLOPT_SSL_VERIFYPEER'];
742
        return $this->generalRequest($params, self::URL_TWITCH . $uri, $method, $postfields);
743
    }
744
745
    /**
746
     * Twitch Team API request
747
     * @param   string
748
     * @param   string
749
     * @param   string
750
     * @return  stdClass
751
     * @throws  \ritero\SDK\TwitchTV\TwitchException
752
     */
753
    private function teamRequest($uri, $method = 'GET', $postfields = null)
754
    {
755
        return $this->generalRequest([], self::URL_TWITCH_TEAM . $uri . '.json', $method, $postfields);
756
    }
757
758
    /**
759
     * TwitchAPI request
760
     * method used by teamRequest && request methods
761
     * because there are two different Twitch APIs
762
     * don't call it directly
763
     * @param   array
764
     * @param   string
765
     * @param   string
766
     * @param   string
767
     * @return  stdClass
768
     * @throws  \ritero\SDK\TwitchTV\TwitchException
769
     */
770
    private function generalRequest($params, $uri, $method = 'GET', $postfields = null)
771
    {
772
        $this->http_info = array();
773
774
        $crl = curl_init();
775
        curl_setopt($crl, CURLOPT_USERAGENT, $this->useragent);
776
        curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
777
        curl_setopt($crl, CURLOPT_TIMEOUT, $this->timeout);
778
        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
779
        curl_setopt($crl, CURLOPT_HTTPHEADER, array('Expect:', 'Client-ID: ' . $this->getClientId()));
780
        if (isset($params['CURLOPT_SSL_VERIFYPEER'])) {
781
            curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
782
        }
783
        curl_setopt($crl, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
784
        curl_setopt($crl, CURLOPT_HEADER, false);
785
786
        switch ($method) {
787
            case 'POST':
788
                curl_setopt($crl, CURLOPT_POST, true);
789
                if (!is_null($postfields)) {
790
                    curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?'));
791
                }
792
                break;
793 View Code Duplication
            case 'PUT':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
794
                curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'PUT');
795
                curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields)));
796
                if (!is_null($postfields)) {
797
                    curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?'));
798
                }
799
                break;
800 View Code Duplication
            case 'DELETE':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
801
                curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'DELETE');
802
                curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields)));
803
                if (!is_null($postfields)) {
804
                    curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?'));
805
                }
806
        }
807
808
        curl_setopt($crl, CURLOPT_URL, $uri);
809
810
        $response = curl_exec($crl);
811
812
        $this->http_code = curl_getinfo($crl, CURLINFO_HTTP_CODE);
813
        $this->http_info = array_merge($this->http_info, curl_getinfo($crl));
814
815
        if (curl_errno($crl) && $this->throw_curl_errors === true) {
816
            throw new TwitchException(curl_error($crl), curl_errno($crl));
817
        }
818
819
        curl_close($crl);
820
821
        return json_decode($response);
822
    }
823
824
    /**
825
     * Get the header info to store
826
     */
827
    private function getHeader($ch, $header)
828
    {
829
        $i = strpos($header, ':');
830
        if (!empty($i)) {
831
            $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
832
            $value = trim(substr($header, $i + 2));
833
            $this->http_header[$key] = $value;
834
        }
835
836
        return strlen($header);
837
    }
838
839
    /**
840
     * Configuration exception
841
     * @throws  \ritero\SDK\TwitchTV\TwitchException
842
     */
843
    private function authConfigException()
844
    {
845
        throw new TwitchException('Cannot call authenticate functions without valid API configuration');
846
    }
847
}
848