Authentication

Inspired by Laravel's Http Clientopen in new window, 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 Authenticationopen in new window.

$response = $client
        ->useBasicAuth('john@doe.org', 'secret')
        ->get('/available-flights');

Basic authentication can also be set via configurationopen in new window.

Digest

Digest Authenticationopen in new window 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 configurationopen in new window.

Token

Lastly, when using token-based authenticationopen in new window, useTokenAuth() can be used. It accepts the following arguments:

  • $token: string The token you wish to use
  • $scheme: string (optional) The authentication scheme. Defaults to Bearer.
 $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 ...
            ]
        ],
    ],
];