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\TwitchException |
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\TwitchException |
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\TwitchException |
128
|
|
|
*/ |
129
|
|
|
private function generalRequest($uri, $method = 'GET', $postfields = null) |
130
|
|
|
{ |
131
|
|
|
$this->httpInfo = array(); |
132
|
|
|
|
133
|
|
|
$crl = curl_init(); |
134
|
|
|
curl_setopt($crl, CURLOPT_USERAGENT, $this->userAgent); |
135
|
|
|
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
136
|
|
|
curl_setopt($crl, CURLOPT_TIMEOUT, $this->timeout); |
137
|
|
|
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); |
138
|
|
|
curl_setopt($crl, CURLOPT_HTTPHEADER, array( |
139
|
|
|
'Expect:', |
140
|
|
|
'Accept: ' . sprintf(self::MIME_TYPE, $this->getApiVersion()), |
141
|
|
|
'Client-ID: ' . $this->getClientId(), |
142
|
|
|
)); |
143
|
|
|
curl_setopt($crl, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); |
144
|
|
|
curl_setopt($crl, CURLOPT_HEADER, false); |
145
|
|
|
|
146
|
|
|
switch ($method) { |
147
|
|
|
case 'POST': |
148
|
|
|
curl_setopt($crl, CURLOPT_POST, true); |
149
|
|
|
break; |
150
|
|
View Code Duplication |
case 'PUT': |
|
|
|
|
151
|
|
|
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'PUT'); |
152
|
|
|
curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields))); |
153
|
|
|
break; |
154
|
|
View Code Duplication |
case 'DELETE': |
|
|
|
|
155
|
|
|
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
156
|
|
|
curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields))); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($postfields !== null) { |
160
|
|
|
curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
curl_setopt($crl, CURLOPT_URL, $uri); |
164
|
|
|
|
165
|
|
|
$response = curl_exec($crl); |
166
|
|
|
|
167
|
|
|
$this->httpCode = curl_getinfo($crl, CURLINFO_HTTP_CODE); |
168
|
|
|
$this->httpInfo = array_merge($this->httpInfo, curl_getinfo($crl)); |
169
|
|
|
|
170
|
|
|
if (curl_errno($crl) && $this->throwCurlErrors === true) { |
171
|
|
|
throw new TwitchException(curl_error($crl), curl_errno($crl)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
curl_close($crl); |
175
|
|
|
|
176
|
|
|
return json_decode($response); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the header info to store |
181
|
|
|
*/ |
182
|
|
|
private function getHeader($ch, $header) |
183
|
|
|
{ |
184
|
|
|
$i = strpos($header, ':'); |
185
|
|
|
if (!empty($i)) { |
186
|
|
|
$key = str_replace('-', '_', strtolower(substr($header, 0, $i))); |
187
|
|
|
$value = trim(substr($header, $i + 2)); |
188
|
|
|
$this->httpHeader[$key] = $value; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return strlen($header); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
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.