You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For example, if you want to add a language header value in all requests:
217
+
218
+
```php
219
+
use ProgrammatorDev\Api\Api;
220
+
221
+
class YourApi extends Api
222
+
{
223
+
public function __construct(string $language = 'en')
224
+
{
225
+
parent::__construct();
226
+
227
+
$this->setBaseUrl('https://api.example.com/v1');
228
+
$this->addHeaderDefault('X-LANGUAGE', $language);
229
+
}
230
+
231
+
public function getPosts(): string
232
+
{
233
+
// GET https://api.example.com/v1/posts with an 'X-LANGUAGE' => 'en' header value
234
+
return $this->request(
235
+
method: 'GET',
236
+
path: '/posts'
237
+
);
238
+
}
239
+
240
+
public function getCategories(): string
241
+
{
242
+
// a header with the same name, passed in the request method, will overwrite a header default
243
+
// GET https://api.example.com/v1/categories with an 'X-LANGUAGE' => 'pt' header value
244
+
return $this->request(
245
+
method: 'GET',
246
+
path: '/categories',
247
+
headers: [
248
+
'X-LANGUAGE' => 'pt'
249
+
]
250
+
);
251
+
}
252
+
}
253
+
```
254
+
255
+
> [!NOTE]
256
+
> A header with the same name, passed in the `request` method, will overwrite a header default.
257
+
> Check the `getCategories` method in the example above.
258
+
259
+
### Authentication
260
+
261
+
Getter and setter for API authentication.
262
+
Uses the [authentication component](https://docs.php-http.org/en/latest/message/authentication.html) from [PHP HTTP](https://docs.php-http.org/en/latest/index.html).
0 commit comments