Authentication
Inspired by Laravel's Http Client, authentication headers can be specified via these shortcut methods.
Caution
Regardless of authentication method, you should always use https. You never know who's listening...
Basic
The useBasicAuth()
method will allow you to set a username and password, for Basic Authentication.
$response = $client
->useBasicAuth('john@doe.org', 'secret')
->get('/available-flights');
Basic authentication can also be set via configuration.
Digest
Digest Authentication can be used, via useDigestAuth()
. This method also accepts a username and a password.
$response = $client
->useDigestAuth('john@doe.org', 'secret')
->get('/available-flights');
Digest authentication can also be set via configuration.
Token
Lastly, when using token-based authentication, useTokenAuth()
can be used. It accepts the following arguments:
$token
:string
The token you wish to use$scheme
:string
(optional) The authentication scheme. Defaults toBearer
.
$response = $client
->useTokenAuth('my-secret-api-token')
->get('/available-flights');
Alternatively, you can also use the configuration to specify authentication headers.
<?php
return [
'profiles' => [
'default' => [
'driver' => \Aedart\Http\Clients\Drivers\DefaultHttpClient::class,
'options' => [
'headers' => [
'Authorization' => 'Bearer my-secret-api-token'
]
// ... remaining not shown ...
]
],
],
];