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
|
|
|
const URL_TWITCH = 'https://api.twitch.tv/kraken/'; |
44
|
|
|
const URL_TWITCH_TEAM = 'http://api.twitch.tv/api/team/'; |
45
|
|
|
const URI_AUTH = 'oauth2/authorize'; |
46
|
|
|
const URI_AUTH_TOKEN = 'oauth2/token'; |
47
|
|
|
const MIME_TYPE = 'application/vnd.twitchtv.v%d+json'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* TwitchRequest constructor |
51
|
|
|
* @param array $config |
52
|
|
|
*/ |
53
|
|
|
public function __construct($config) |
54
|
|
|
{ |
55
|
|
|
$this->config = $config; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the API version to use |
60
|
|
|
* @param integer $version |
61
|
|
|
* @deprecated will be removed, force to use v3 API, which is current stable Twitch API version |
62
|
|
|
*/ |
63
|
|
|
public function setApiVersion($version) |
64
|
|
|
{ |
65
|
|
|
if (ctype_digit(strval($version))) { |
66
|
|
|
$this->apiVersion = (int)$version; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the API version |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function getApiVersion() |
75
|
|
|
{ |
76
|
|
|
return $this->apiVersion; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* TwitchAPI request |
81
|
|
|
* @param string |
82
|
|
|
* @param string |
83
|
|
|
* @param string |
84
|
|
|
* @return \stdClass |
85
|
|
|
* @throws \jofner\SDK\TwitchTV\TwitchException |
86
|
|
|
*/ |
87
|
|
|
public function request($uri, $method = 'GET', $postfields = null) |
88
|
|
|
{ |
89
|
|
|
return $this->generalRequest(self::URL_TWITCH . $uri, $method, $postfields); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Twitch Team API request |
94
|
|
|
* @param string |
95
|
|
|
* @param string |
96
|
|
|
* @param string |
97
|
|
|
* @return \stdClass |
98
|
|
|
* @throws \jofner\SDK\TwitchTV\TwitchException |
99
|
|
|
*/ |
100
|
|
|
public function teamRequest($uri, $method = 'GET', $postfields = null) |
101
|
|
|
{ |
102
|
|
|
return $this->generalRequest(self::URL_TWITCH_TEAM . $uri . '.json', $method, $postfields); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* TwitchAPI request |
107
|
|
|
* method used by teamRequest && request methods |
108
|
|
|
* because there are two different Twitch APIs |
109
|
|
|
* don't call it directly |
110
|
|
|
* @param array |
111
|
|
|
* @param string |
112
|
|
|
* @param string |
113
|
|
|
* @param string |
114
|
|
|
* @return \stdClass |
115
|
|
|
* @throws \jofner\SDK\TwitchTV\TwitchException |
116
|
|
|
*/ |
117
|
|
|
private function generalRequest($uri, $method = 'GET', $postfields = null) |
118
|
|
|
{ |
119
|
|
|
$this->httpInfo = array(); |
120
|
|
|
|
121
|
|
|
$crl = curl_init(); |
122
|
|
|
curl_setopt($crl, CURLOPT_USERAGENT, $this->userAgent); |
123
|
|
|
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
124
|
|
|
curl_setopt($crl, CURLOPT_TIMEOUT, $this->timeout); |
125
|
|
|
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); |
126
|
|
|
curl_setopt($crl, CURLOPT_HTTPHEADER, array('Expect:', 'Accept: ' . sprintf(self::MIME_TYPE, $this->getApiVersion()), 'Client-ID: ' . $this->config['client_id'])); |
127
|
|
|
curl_setopt($crl, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); |
128
|
|
|
curl_setopt($crl, CURLOPT_HEADER, false); |
129
|
|
|
|
130
|
|
|
switch ($method) { |
131
|
|
|
case 'POST': |
132
|
|
|
curl_setopt($crl, CURLOPT_POST, true); |
133
|
|
|
if ($postfields !== null) { |
134
|
|
|
curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?')); |
135
|
|
|
} |
136
|
|
|
break; |
137
|
|
View Code Duplication |
case 'PUT': |
|
|
|
|
138
|
|
|
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'PUT'); |
139
|
|
|
curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields))); |
140
|
|
|
if ($postfields !== null) { |
141
|
|
|
curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?')); |
142
|
|
|
} |
143
|
|
|
break; |
144
|
|
View Code Duplication |
case 'DELETE': |
|
|
|
|
145
|
|
|
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
146
|
|
|
curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields))); |
147
|
|
|
if ($postfields !== null) { |
148
|
|
|
curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?')); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
curl_setopt($crl, CURLOPT_URL, $uri); |
153
|
|
|
|
154
|
|
|
$response = curl_exec($crl); |
155
|
|
|
|
156
|
|
|
$this->httpCode = curl_getinfo($crl, CURLINFO_HTTP_CODE); |
157
|
|
|
$this->httpInfo = array_merge($this->httpInfo, curl_getinfo($crl)); |
158
|
|
|
|
159
|
|
|
if (curl_errno($crl) && $this->throwCurlErrors === true) { |
160
|
|
|
throw new TwitchException(curl_error($crl), curl_errno($crl)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
curl_close($crl); |
164
|
|
|
|
165
|
|
|
return json_decode($response); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the header info to store |
170
|
|
|
*/ |
171
|
|
|
private function getHeader($ch, $header) |
172
|
|
|
{ |
173
|
|
|
$i = strpos($header, ':'); |
174
|
|
|
if (!empty($i)) { |
175
|
|
|
$key = str_replace('-', '_', strtolower(substr($header, 0, $i))); |
176
|
|
|
$value = trim(substr($header, $i + 2)); |
177
|
|
|
$this->httpHeader[$key] = $value; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return strlen($header); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: