\GuzzleHttpMiddleware

Functions used to create and wrap handlers with handler middleware.

Summary

Methods
Properties
Constants
cookies()
httpErrors()
history()
tap()
redirect()
retry()
log()
prepareBody()
mapRequest()
mapResponse()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

cookies()

cookies() : callable

Middleware that adds cookies to requests.

The options array must be set to a CookieJarInterface in order to use cookies. This is typically handled for you by a client.

Returns

callable —

Returns a function that accepts the next handler.

httpErrors()

httpErrors() : callable

Middleware that throws exceptions for 4xx or 5xx responses when the "http_error" request option is set to true.

Returns

callable —

Returns a function that accepts the next handler.

history()

history(array  $container) : callable

Middleware that pushes history data to an ArrayAccess container.

Parameters

array $container

Container to hold the history (by reference).

Throws

\InvalidArgumentException

if container is not an array or ArrayAccess.

Returns

callable —

Returns a function that accepts the next handler.

tap()

tap(callable  $before = null, callable  $after = null) : callable

Middleware that invokes a callback before and after sending a request.

The provided listener cannot modify or alter the response. It simply "taps" into the chain to be notified before returning the promise. The before listener accepts a request and options array, and the after listener accepts a request, options array, and response promise.

Parameters

callable $before

Function to invoke before forwarding the request.

callable $after

Function invoked after forwarding.

Returns

callable —

Returns a function that accepts the next handler.

redirect()

redirect() : callable

Middleware that handles request redirects.

Returns

callable —

Returns a function that accepts the next handler.

retry()

retry(callable  $decider, callable  $delay = null) : callable

Middleware that retries requests based on the boolean result of invoking the provided "decider" function.

If no delay function is provided, a simple implementation of exponential backoff will be utilized.

Parameters

callable $decider

Function that accepts the number of retries, a request, [response], and [exception] and returns true if the request is to be retried.

callable $delay

Function that accepts the number of retries and returns the number of milliseconds to delay.

Returns

callable —

Returns a function that accepts the next handler.

log()

log(\Psr\Log\LoggerInterface  $logger, \GuzzleHttp\MessageFormatter  $formatter, string  $logLevel = \Psr\Log\LogLevel::INFO) : callable

Middleware that logs requests, responses, and errors using a message formatter.

Parameters

\Psr\Log\LoggerInterface $logger

Logs messages.

\GuzzleHttp\MessageFormatter $formatter

Formatter used to create message strings.

string $logLevel

Level at which to log requests.

Returns

callable —

Returns a function that accepts the next handler.

prepareBody()

prepareBody() : callable

This middleware adds a default content-type if possible, a default content-length or transfer-encoding header, and the expect header.

Returns

callable

mapRequest()

mapRequest(callable  $fn) : callable

Middleware that applies a map function to the request before passing to the next handler.

Parameters

callable $fn

Function that accepts a RequestInterface and returns a RequestInterface.

Returns

callable

mapResponse()

mapResponse(callable  $fn) : callable

Middleware that applies a map function to the resolved promise's response.

Parameters

callable $fn

Function that accepts a ResponseInterface and returns a ResponseInterface.

Returns

callable