Completed
Push — develop ( 6573d3...05d4b9 )
by Josef
01:30
created

TwitchRequest::teamRequest()   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 3
1
<?php
2
3
namespace jofner\SDK\TwitchTV;
4
5
/**
6
 * TwitchRequest for 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
 */
14
class TwitchRequest
15
{
16
    /** @var string Set the useragnet */
17
    private $userAgent = 'jofner TwitchSDK 2.*';
18
19
    /** @var integer Set connect timeout */
20
    public $connectTimeout = 30;
21
22
    /** @var integer Set timeout default. */
23
    public $timeout = 30;
24
25
    /** @var boolean Verify SSL Cert */
26
    public $sslVerifypeer = false;
27
28
    /** @var integer Contains the last HTTP status code returned */
29
    public $httpCode = 0;
30
31
    /** @var array Contains the last HTTP headers returned */
32
    public $httpInfo = array();
33
34
    /** @var array Contains the last Server headers returned */
35
    public $httpHeader = array();
36
37
    /** @var boolean Throw cURL errors */
38
    public $throwCurlErrors = true;
39
40
    /** @var int API version to use */
41
    private $apiVersion = 3;
42
43
    /** @var string */
44
    private $clientId;
45
46
    const URL_TWITCH = 'https://api.twitch.tv/kraken/';
47
    const URL_TWITCH_TEAM = 'http://api.twitch.tv/api/team/';
48
    const URI_AUTH = 'oauth2/authorize';
49
    const URI_AUTH_TOKEN = 'oauth2/token';
50
    const MIME_TYPE = 'application/vnd.twitchtv.v%d+json';
51
52
    /**
53
     * Set the API version to use
54
     * @param integer $version
55
     * @deprecated will be removed, force to use v3 API, which is current stable Twitch API version
56
     */
57
    public function setApiVersion($version)
58
    {
59
        if (ctype_digit(strval($version))) {
60
            $this->apiVersion = (int)$version;
61
        }
62
    }
63
64
    /**
65
     * Get the API version
66
     * @return int
67
     */
68
    public function getApiVersion()
69
    {
70
        return $this->apiVersion;
71
    }
72
73
    /**
74
     * Get Client ID
75
     * @return string
76
     */
77
    public function getClientId()
78
    {
79
        return $this->clientId;
80
    }
81
82
    /**
83
     * Set CLient ID
84
     * @param string $clientId
85
     */
86
    public function setClientId($clientId)
87
    {
88
        $this->clientId = $clientId;
89
    }
90
91
    /**
92
     * TwitchAPI request
93
     * @param   string
94
     * @param   string
95
     * @param   string
96
     * @return  \stdClass
97
     * @throws  \jofner\SDK\TwitchTV\TwitchSDKException
98
     */
99
    public function request($uri, $method = 'GET', $postfields = null)
100
    {
101
        return $this->generalRequest(self::URL_TWITCH . $uri, $method, $postfields);
102
    }
103
104
    /**
105
     * Twitch Team API request
106
     * @param   string
107
     * @param   string
108
     * @param   string
109
     * @return  \stdClass
110
     * @throws  \jofner\SDK\TwitchTV\TwitchSDKException
111
     */
112
    public function teamRequest($uri, $method = 'GET', $postfields = null)
113
    {
114
        return $this->generalRequest(self::URL_TWITCH_TEAM . $uri . '.json', $method, $postfields);
115
    }
116
117
    /**
118
     * TwitchAPI request
119
     * method used by teamRequest && request methods
120
     * because there are two different Twitch APIs
121
     * don't call it directly
122
     * @param   array
123
     * @param   string
124
     * @param   string
125
     * @param   string
126
     * @return  \stdClass
127
     * @throws  \jofner\SDK\TwitchTV\TwitchSDKException
128
     */
129
    private function generalRequest($uri, $method = 'GET', $postfields = null)
130
    {
131
        $this->httpInfo = array();
132
133
        $crl = $this->initCrl($uri, $method, $postfields);
134
        $response = curl_exec($crl);
135
136
        $this->httpCode = curl_getinfo($crl, CURLINFO_HTTP_CODE);
137
        $this->httpInfo = array_merge($this->httpInfo, curl_getinfo($crl));
138
139
        if (curl_errno($crl) && $this->throwCurlErrors === true) {
140
            throw new TwitchSDKException(curl_error($crl), curl_errno($crl));
141
        }
142
143
        curl_close($crl);
144
145
        return json_decode($response);
146
    }
147
148
    /**
149
     * Initialize a cURL session
150
     * @param string $uri
151
     * @param string $method
152
     * @param array|null $postfields
153
     * @return resource
154
     */
155
    private function initCrl($uri, $method, $postfields)
156
    {
157
        $optHttpHeader = array(
158
            'Expect:',
159
            'Accept: ' . sprintf(self::MIME_TYPE, $this->getApiVersion()),
160
            'Client-ID: ' . $this->getClientId(),
161
        );
162
163
        $crl = curl_init();
164
        curl_setopt($crl, CURLOPT_USERAGENT, $this->userAgent);
165
        curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
166
        curl_setopt($crl, CURLOPT_TIMEOUT, $this->timeout);
167
        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
168
        curl_setopt($crl, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
169
        curl_setopt($crl, CURLOPT_HEADER, false);
170
171
        switch ($method) {
172
            case 'POST':
173
                curl_setopt($crl, CURLOPT_POST, true);
174
                break;
175
            case 'PUT':
176
                curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'PUT');
177
                break;
178
            case 'DELETE':
179
                curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'DELETE');
180
        }
181
182
        if ($postfields !== null) {
183
            curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?'));
184
            $optHttpHeader[] = 'Content-Length: ' . strlen($postfields);
185
        }
186
187
        curl_setopt($crl, CURLOPT_HTTPHEADER, $optHttpHeader);
188
        curl_setopt($crl, CURLOPT_URL, $uri);
189
190
        return $crl;
191
    }
192
193
    /**
194
     * Get the header info to store
195
     */
196
    private function getHeader($ch, $header)
197
    {
198
        $i = strpos($header, ':');
199
        if (!empty($i)) {
200
            $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
201
            $value = trim(substr($header, $i + 2));
202
            $this->httpHeader[$key] = $value;
203
        }
204
205
        return strlen($header);
206
    }
207
}
208