123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- =================
- Client Middleware
- =================
- Middleware intercepts requests before they are sent over the wire and can be
- used to add functionality to handlers.
- Modifying Requests
- ------------------
- Let's say you wanted to modify requests before they are sent over the wire
- so that they always add specific headers. This can be accomplished by creating
- a function that accepts a handler and returns a new function that adds the
- composed behavior.
- .. code-block:: php
- use GuzzleHttp\Ring\Client\CurlHandler;
- $handler = new CurlHandler();
- $addHeaderHandler = function (callable $handler, array $headers = []) {
- return function (array $request) use ($handler, $headers) {
- // Add our custom headers
- foreach ($headers as $key => $value) {
- $request['headers'][$key] = $value;
- }
- // Send the request using the handler and return the response.
- return $handler($request);
- }
- };
- // Create a new handler that adds headers to each request.
- $handler = $addHeaderHandler($handler, [
- 'X-AddMe' => 'hello',
- 'Authorization' => 'Basic xyz'
- ]);
- $response = $handler([
- 'http_method' => 'GET',
- 'headers' => ['Host' => ['httpbin.org']]
- ]);
- Modifying Responses
- -------------------
- You can change a response as it's returned from a middleware. Remember that
- responses returned from an handler (including middleware) must implement
- ``GuzzleHttp\Ring\Future\FutureArrayInterface``. In order to be a good citizen,
- you should not expect that the responses returned through your middleware will
- be completed synchronously. Instead, you should use the
- ``GuzzleHttp\Ring\Core::proxy()`` function to modify the response when the
- underlying promise is resolved. This function is a helper function that makes it
- easy to create a new instance of ``FutureArrayInterface`` that wraps an existing
- ``FutureArrayInterface`` object.
- Let's say you wanted to add headers to a response as they are returned from
- your middleware, but you want to make sure you aren't causing future
- responses to be dereferenced right away. You can achieve this by modifying the
- incoming request and using the ``Core::proxy`` function.
- .. code-block:: php
- use GuzzleHttp\Ring\Core;
- use GuzzleHttp\Ring\Client\CurlHandler;
- $handler = new CurlHandler();
- $responseHeaderHandler = function (callable $handler, array $headers) {
- return function (array $request) use ($handler, $headers) {
- // Send the request using the wrapped handler.
- return Core::proxy($handler($request), function ($response) use ($headers) {
- // Add the headers to the response when it is available.
- foreach ($headers as $key => $value) {
- $response['headers'][$key] = (array) $value;
- }
- // Note that you can return a regular response array when using
- // the proxy method.
- return $response;
- });
- }
- };
- // Create a new handler that adds headers to each response.
- $handler = $responseHeaderHandler($handler, ['X-Header' => 'hello!']);
- $response = $handler([
- 'http_method' => 'GET',
- 'headers' => ['Host' => ['httpbin.org']]
- ]);
- assert($response['headers']['X-Header'] == 'hello!');
- Built-In Middleware
- -------------------
- RingPHP comes with a few basic client middlewares that modify requests
- and responses.
- Streaming Middleware
- ~~~~~~~~~~~~~~~~~~~~
- If you want to send all requests with the ``streaming`` option to a specific
- handler but other requests to a different handler, then use the streaming
- middleware.
- .. code-block:: php
- use GuzzleHttp\Ring\Client\CurlHandler;
- use GuzzleHttp\Ring\Client\StreamHandler;
- use GuzzleHttp\Ring\Client\Middleware;
- $defaultHandler = new CurlHandler();
- $streamingHandler = new StreamHandler();
- $streamingHandler = Middleware::wrapStreaming(
- $defaultHandler,
- $streamingHandler
- );
- // Send the request using the streaming handler.
- $response = $streamingHandler([
- 'http_method' => 'GET',
- 'headers' => ['Host' => ['www.google.com']],
- 'stream' => true
- ]);
- // Send the request using the default handler.
- $response = $streamingHandler([
- 'http_method' => 'GET',
- 'headers' => ['Host' => ['www.google.com']]
- ]);
- Future Middleware
- ~~~~~~~~~~~~~~~~~
- If you want to send all requests with the ``future`` option to a specific
- handler but other requests to a different handler, then use the future
- middleware.
- .. code-block:: php
- use GuzzleHttp\Ring\Client\CurlHandler;
- use GuzzleHttp\Ring\Client\CurlMultiHandler;
- use GuzzleHttp\Ring\Client\Middleware;
- $defaultHandler = new CurlHandler();
- $futureHandler = new CurlMultiHandler();
- $futureHandler = Middleware::wrapFuture(
- $defaultHandler,
- $futureHandler
- );
- // Send the request using the blocking CurlHandler.
- $response = $futureHandler([
- 'http_method' => 'GET',
- 'headers' => ['Host' => ['www.google.com']]
- ]);
- // Send the request using the non-blocking CurlMultiHandler.
- $response = $futureHandler([
- 'http_method' => 'GET',
- 'headers' => ['Host' => ['www.google.com']],
- 'future' => true
- ]);
|