Laravel Guzzle - Laravel Guzzle HTTP Client Manager
This package provides a mechanism to configure one or more Guzzle clients for use in an
application., (*1)
Installation
Install with:, (*2)
composer require jcsilkey/laravel-guzzle
, (*3)
With auto package discovery, the ServiceProvider is automatically registered.
No facade is or ever will be provided because facades are evil., (*4)
Publish the configuration:, (*5)
php artisan vendor:publish --tag="config"
, (*6)
Usage
This package registers 2 services:, (*7)
GuzzleHttp\ClientInterface
Resolves to the default client defined in the package configuration., (*8)
JCS\LaravelGuzzle\GuzzleClientRegistry
Resolves to the client registry, which holds all clients. GuzzleClientRegistry@getClient()
will return the default client. Pass a client name to get a specific client, GuzzleClientRegistry@getClient($clientName)
., (*9)
Configuration
The default configuration is minimal:, (*10)
return [
'default' => env('GUZZLE_DEFAULT_CLIENT', null),
'global' => [],
'clients' => []
]
];
default
The name of the default client. If this value is null
, then a client named 'default' will be created using the settings defined in global
. If a name is given, then it must be a valid client defined in clients
., (*11)
global
Default configuration options to be used for all clients. Any valid Guzzle configuration option can be used., (*12)
clients
An array of named clients, along with configuration options for each. For example:, (*13)
'clients' => [
'example_api' => [
'base_uri' => 'https://api.example.com'
],
'example_api2' => [
'base_uri' => 'https://api2.example.com'
],
]
Client options are merged with the global options defined above (shallow merge, top level keys only)., (*14)
Configuring Handlers and Middlewares
To define a custom Handler and Guzzle Middleware stack, use the normal guzzle handler
option to specify a handler class and a special middleware
option that is an array of middlewares to be added to your client. Each middleware definition is itself an array consisting of a callable
and the name
to register for the middleware. For example:, (*15)
`handler` => 'GuzzleHttp\Handler\CurlHandler',
'middleware' => [
[
'callable' => 'GuzzleHttp\Middleware::httpErrors',
'name' => 'http_errors'
],
]
If no handler
is specified, one will be automatically selected for you based on your PHP configuration., (*16)
If no middleware
are defined, your Guzzle client will be created using the default, (*17)