Skip to content

Commit ef9b92e

Browse files
committed
Add default_headers option
1 parent cec26e1 commit ef9b92e

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

docs/examples/setting-options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ There are a few options that can be used to control the behaviour of the API. Al
55
```php
66
$options = [
77
'auto_refresh' => true,
8+
'default_headers' => ['Accept-Language' => 'en-US'],
89
];
910

1011
// Options can be set using the SpotifyWebAPI constructor
@@ -28,6 +29,12 @@ Used to control [automatic refresh of access tokens](refreshing-access-tokens.md
2829

2930
Used to control automatic retries of [rate limited requests](https://developer.spotify.com/documentation/web-api/guides/rate-limits/).
3031

32+
### `default_headers`
33+
34+
* Possible values: `[]` (default)
35+
36+
Used to set default HTTP headers that will be included in every request. For example `Accept-Language` to force Latin alphabet in returned results. Passed as key-value pairs, e.g. `['Accept-Language' => 'en-US']`.
37+
3138
### `return_assoc`
3239

3340
* Possible values: `true`/`false` (default)

src/Request.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Request
1212
protected array $lastResponse = [];
1313
protected array $options = [
1414
'curl_options' => [],
15+
'default_headers' => [],
1516
'return_assoc' => false,
1617
];
1718

@@ -26,6 +27,25 @@ public function __construct(array|object $options = [])
2627
$this->setOptions($options);
2728
}
2829

30+
/**
31+
* Format request headers to be used with cURL.
32+
*
33+
* @param array|object $headers The headers to format.
34+
*
35+
* @return array The formatted headers.
36+
*/
37+
protected function formatHeaders(array|object $headers): array
38+
{
39+
$headers = array_merge((array) $this->options['default_headers'], $headers);
40+
41+
$formattedHeaders = [];
42+
foreach ($headers as $key => $val) {
43+
$formattedHeaders[] = "$key: $val";
44+
}
45+
46+
return $formattedHeaders;
47+
}
48+
2949
/**
3050
* Handle response errors.
3151
*
@@ -181,21 +201,16 @@ public function send(string $method, string $url, string|array|object $parameter
181201
$parameters = http_build_query($parameters, '', '&');
182202
}
183203

204+
$method = strtoupper($method);
184205
$options = [
185206
CURLOPT_CAINFO => __DIR__ . '/cacert.pem',
186207
CURLOPT_ENCODING => '',
187208
CURLOPT_HEADER => true,
188-
CURLOPT_HTTPHEADER => [],
209+
CURLOPT_HTTPHEADER => $this->formatHeaders($headers),
189210
CURLOPT_RETURNTRANSFER => true,
190211
CURLOPT_URL => rtrim($url, '/'),
191212
];
192213

193-
foreach ($headers as $key => $val) {
194-
$options[CURLOPT_HTTPHEADER][] = "$key: $val";
195-
}
196-
197-
$method = strtoupper($method);
198-
199214
switch ($method) {
200215
case 'DELETE': // No break
201216
case 'PUT':

src/SpotifyWebAPI.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class SpotifyWebAPI
1111
protected array $options = [
1212
'auto_refresh' => false,
1313
'auto_retry' => false,
14+
'default_headers' => [],
1415
'return_assoc' => false,
1516
];
1617
protected ?Request $request = null;
@@ -113,6 +114,7 @@ protected function sendRequest(
113114
array $headers = []
114115
): array {
115116
$this->request->setOptions([
117+
'default_headers' => $this->options['default_headers'],
116118
'return_assoc' => $this->options['return_assoc'],
117119
]);
118120

0 commit comments

Comments
 (0)