Middleware in Guzzle is a powerful feature that allows you to augment and manipulate the HTTP requests and responses in your application. Middleware in Guzzle is essentially a piece of code that sits between the point where a request is created and the point where it is sent, and similarly, between the point where a response is received and the point where it is returned to the client.
Middleware functions are used to modify the request or response objects, add additional functionality like logging, caching, authentication, error handling, and more. They can be used to implement complex request/response manipulations in a clean and reusable way.
In Guzzle, middleware are composed together using a handler stack. Each middleware can be thought of as a layer in an onion, where requests go out from the center and responses come back in from the outside. When you send a request, it goes through each middleware layer, starting with the outermost layer and proceeding inward. When a response comes back, it goes through the middleware layers in the opposite order.
Here's an example of how to create and use middleware in Guzzle:
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
// Create a handler stack
$stack = new HandlerStack();
$stack->setHandler(\GuzzleHttp\choose_handler());
// Create a middleware that adds a header to the request
$headerMiddleware = Middleware::mapRequest(function ($request) {
// Add a custom header to the request
return $request->withHeader('X-Custom-Header', 'MyValue');
});
// Add the middleware to the handler stack
$stack->push($headerMiddleware, 'header_middleware');
// Create a Guzzle client with the handler stack
$client = new Client(['handler' => $stack]);
// Send a request
$response = $client->request('GET', 'http://httpbin.org/get');
// Output the response body
echo $response->getBody();
In this example, we create a custom middleware $headerMiddleware
that adds a custom 'X-Custom-Header' to every request. This middleware is added to the handler stack $stack
which is then passed to the Guzzle client. When a request is made using this client, the middleware function is invoked and modifies the request accordingly.
Middleware functions can also be used to process responses. Here's an example of a middleware that checks for a certain condition in the response:
// Create a middleware that checks the response
$responseMiddleware = Middleware::mapResponse(function ($response) {
// Check for a condition in the response
if ($response->getStatusCode() == 200) {
// Do something if the condition is met
}
return $response;
});
// Add the middleware to the handler stack
$stack->push($responseMiddleware, 'response_middleware');
In this case, the $responseMiddleware
middleware checks the status code of the response and can perform some action based on it.
Middleware can be very useful for creating reusable components that can be shared across multiple projects or requests within a project. They can simplify complex request/response logic and make your codebase cleaner and more maintainable.