forked from jwilsson/spotify-web-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
253 lines (215 loc) · 7.79 KB
/
Copy pathRequest.php
File metadata and controls
253 lines (215 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace SpotifyWebAPI;
class Request
{
const ACCOUNT_URL = 'https://accounts.spotify.com';
const API_URL = 'https://api.spotify.com';
const RETURN_ASSOC = 'assoc';
const RETURN_OBJECT = 'object';
protected $lastResponse = [];
protected $returnType = self::RETURN_OBJECT;
/**
* Parse the response body and handle API errors.
*
* @param string $body The raw, unparsed response body.
* @param int $status The HTTP status code, used to see if additional error handling is needed.
*
* @throws SpotifyWebAPIException
* @throws SpotifyWebAPIAuthException
*
* @return array|object The parsed response body. Type is controlled by `Request::setReturnType()`.
*/
protected function parseBody($body, $status)
{
$this->lastResponse['body'] = json_decode($body, $this->returnType == self::RETURN_ASSOC);
if ($status >= 200 && $status <= 299) {
return $this->lastResponse['body'];
}
$body = json_decode($body);
$error = isset($body->error) ? $body->error : null;
if (isset($error->message) && isset($error->status)) {
// API call error
throw new SpotifyWebAPIException($error->message, $error->status);
} elseif (isset($body->error_description)) {
// Auth call error
throw new SpotifyWebAPIAuthException($body->error_description, $status);
} else {
// Something went really wrong
throw new SpotifyWebAPIException('An unknown error occurred.', $status);
}
}
/**
* Parse HTTP response headers.
*
* @param string $headers The raw, unparsed response headers.
*
* @return array Headers as key–value pairs.
*/
protected function parseHeaders($headers)
{
$headers = str_replace("\r\n", "\n", $headers);
$headers = explode("\n", $headers);
array_shift($headers);
$parsedHeaders = [];
foreach ($headers as $header) {
list($key, $value) = explode(':', $header, 2);
$parsedHeaders[$key] = trim($value);
}
return $parsedHeaders;
}
/**
* Make a request to the "account" endpoint.
*
* @param string $method The HTTP method to use.
* @param string $uri The URI to request.
* @param array $parameters Optional. Query string parameters or HTTP body, depending on $method.
* @param array $headers Optional. HTTP headers.
*
* @throws SpotifyWebAPIException
* @throws SpotifyWebAPIAuthException
*
* @return array Response data.
* - array|object body The response body. Type is controlled by `Request::setReturnType()`.
* - array headers Response headers.
* - int status HTTP status code.
* - string url The requested URL.
*/
public function account($method, $uri, $parameters = [], $headers = [])
{
return $this->send($method, self::ACCOUNT_URL . $uri, $parameters, $headers);
}
/**
* Make a request to the "api" endpoint.
*
* @param string $method The HTTP method to use.
* @param string $uri The URI to request.
* @param array $parameters Optional. Query string parameters or HTTP body, depending on $method.
* @param array $headers Optional. HTTP headers.
*
* @throws SpotifyWebAPIException
* @throws SpotifyWebAPIAuthException
*
* @return array Response data.
* - array|object body The response body. Type is controlled by `Request::setReturnType()`.
* - array headers Response headers.
* - int status HTTP status code.
* - string url The requested URL.
*/
public function api($method, $uri, $parameters = [], $headers = [])
{
return $this->send($method, self::API_URL . $uri, $parameters, $headers);
}
/**
* Get the latest full response from the Spotify API.
*
* @return array Response data.
* - array|object body The response body. Type is controlled by `Request::setReturnType()`.
* - array headers Response headers.
* - int status HTTP status code.
* - string url The requested URL.
*/
public function getLastResponse()
{
return $this->lastResponse;
}
/**
* Get a value indicating the response body type.
*
* @return string A value indicating if the response body is an object or associative array.
*/
public function getReturnType()
{
return $this->returnType;
}
/**
* Make a request to Spotify.
* You'll probably want to use one of the convenience methods instead.
*
* @param string $method The HTTP method to use.
* @param string $url The URL to request.
* @param array $parameters Optional. Query string parameters or HTTP body, depending on $method.
* @param array $headers Optional. HTTP headers.
*
* @throws SpotifyWebAPIException
* @throws SpotifyWebAPIAuthException
*
* @return array Response data.
* - array|object body The response body. Type is controlled by `Request::setReturnType()`.
* - array headers Response headers.
* - int status HTTP status code.
* - string url The requested URL.
*/
public function send($method, $url, $parameters = [], $headers = [])
{
// Reset any old responses
$this->lastResponse = [];
// Sometimes a stringified JSON object is passed
if (is_array($parameters) || is_object($parameters)) {
$parameters = http_build_query($parameters);
}
$mergedHeaders = [];
foreach ($headers as $key => $val) {
$mergedHeaders[] = "$key: $val";
}
$options = [
CURLOPT_CAINFO => __DIR__ . '/cacert.pem',
CURLOPT_ENCODING => '',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $mergedHeaders,
CURLOPT_RETURNTRANSFER => true,
];
$url = rtrim($url, '/');
$method = strtoupper($method);
switch ($method) {
case 'DELETE': // No break
case 'PUT':
$options[CURLOPT_CUSTOMREQUEST] = $method;
$options[CURLOPT_POSTFIELDS] = $parameters;
break;
case 'POST':
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $parameters;
break;
default:
$options[CURLOPT_CUSTOMREQUEST] = $method;
if ($parameters) {
$url .= '/?' . $parameters;
}
break;
}
$options[CURLOPT_URL] = $url;
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (curl_error($ch)) {
throw new SpotifyWebAPIException('cURL transport error: ' . curl_errno($ch) . ' ' . curl_error($ch));
}
list($headers, $body) = explode("\r\n\r\n", $response, 2);
// Skip the first set of headers for proxied requests
if (preg_match('/^HTTP\/1\.\d 200 Connection established$/', $headers) === 1) {
list($headers, $body) = explode("\r\n\r\n", $body, 2);
}
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headers = $this->parseHeaders($headers);
$this->lastResponse = [
'headers' => $headers,
'status' => $status,
'url' => $url,
];
// Run this here since we might throw
$body = $this->parseBody($body, $status);
curl_close($ch);
return $this->lastResponse;
}
/**
* Set the return type for the response body.
*
* @param string $returnType One of the `Request::RETURN_*` constants.
*
* @return void
*/
public function setReturnType($returnType)
{
$this->returnType = $returnType;
}
}