Learn how to use middleware in the Instructor HTTP client API.
HttpMiddleware
interface:
handle
method takes two parameters:
$request
: The HTTP request to process$next
: The next handler in the middleware chainHttpMiddleware
interface directly, the library provides a convenient BaseMiddleware
abstract class that makes it easier to create middleware:
BaseMiddleware
, you only need to override the methods relevant to your middleware’s functionality, making the code more focused and maintainable.
MiddlewareStack
class manages the collection of middleware components. It provides methods to add, remove, and arrange middleware in the stack.
DebugMiddleware
logs detailed information about HTTP requests and responses:
config/debug.php
file:
BufferResponseMiddleware
stores response bodies and streaming chunks for reuse:
StreamByLineMiddleware
processes streaming responses line by line:
RecordReplayMiddleware
records HTTP interactions and can replay them later:
MODE_PASS
: Normal operation, no recording or replayingMODE_RECORD
: Records all HTTP interactions to the storage directoryMODE_REPLAY
: Replays recorded interactions instead of making real requests