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 $uri |
94
|
|
|
* @param string $method |
95
|
|
|
* @param string $postfields |
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 $uri |
107
|
|
|
* @param string $method |
108
|
|
|
* @param string $postfields |
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 string $uri |
123
|
|
|
* @param string $method |
124
|
|
|
* @param string $postfields |
125
|
|
|
* @return \stdClass |
126
|
|
|
* @throws \jofner\SDK\TwitchTV\TwitchSDKException |
127
|
|
|
*/ |
128
|
|
|
private function generalRequest($uri, $method = 'GET', $postfields = null) |
129
|
|
|
{ |
130
|
|
|
$this->httpInfo = array(); |
131
|
|
|
|
132
|
|
|
$crl = $this->initCrl($uri, $method, $postfields); |
133
|
|
|
$response = curl_exec($crl); |
134
|
|
|
|
135
|
|
|
$this->httpCode = curl_getinfo($crl, CURLINFO_HTTP_CODE); |
136
|
|
|
$this->httpInfo = array_merge($this->httpInfo, curl_getinfo($crl)); |
137
|
|
|
|
138
|
|
|
if (curl_errno($crl) && $this->throwCurlErrors === true) { |
139
|
|
|
throw new TwitchSDKException(curl_error($crl), curl_errno($crl)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
curl_close($crl); |
143
|
|
|
|
144
|
|
|
return json_decode($response); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Initialize a cURL session |
149
|
|
|
* @param string $uri |
150
|
|
|
* @param string $method |
151
|
|
|
* @param string|null $postfields |
152
|
|
|
* @return resource |
153
|
|
|
*/ |
154
|
|
|
private function initCrl($uri, $method, $postfields) |
155
|
|
|
{ |
156
|
|
|
$optHttpHeader = array( |
157
|
|
|
'Expect:', |
158
|
|
|
'Accept: ' . sprintf(self::MIME_TYPE, $this->getApiVersion()), |
159
|
|
|
'Client-ID: ' . $this->getClientId(), |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
$crl = curl_init(); |
163
|
|
|
curl_setopt($crl, CURLOPT_USERAGENT, $this->userAgent); |
164
|
|
|
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
165
|
|
|
curl_setopt($crl, CURLOPT_TIMEOUT, $this->timeout); |
166
|
|
|
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); |
167
|
|
|
curl_setopt($crl, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); |
168
|
|
|
curl_setopt($crl, CURLOPT_HEADER, false); |
169
|
|
|
|
170
|
|
|
switch ($method) { |
171
|
|
|
case 'POST': |
172
|
|
|
curl_setopt($crl, CURLOPT_POST, true); |
173
|
|
|
break; |
174
|
|
|
case 'PUT': |
175
|
|
|
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'PUT'); |
176
|
|
|
break; |
177
|
|
|
case 'DELETE': |
178
|
|
|
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($postfields !== null) { |
182
|
|
|
curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?')); |
183
|
|
|
$optHttpHeader[] = 'Content-Length: ' . strlen($postfields); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
curl_setopt($crl, CURLOPT_HTTPHEADER, $optHttpHeader); |
187
|
|
|
curl_setopt($crl, CURLOPT_URL, $uri); |
188
|
|
|
|
189
|
|
|
return $crl; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the header info to store |
194
|
|
|
* @param $ch |
195
|
|
|
* @param $header |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
|
|
private function getHeader($ch, $header) |
199
|
|
|
{ |
200
|
|
|
$i = strpos($header, ':'); |
201
|
|
|
if (!empty($i)) { |
202
|
|
|
$key = str_replace('-', '_', strtolower(substr($header, 0, $i))); |
203
|
|
|
$value = trim(substr($header, $i + 2)); |
204
|
|
|
$this->httpHeader[$key] = $value; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return strlen($header); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|