Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 2.43 KB

advanced-options.md

File metadata and controls

44 lines (31 loc) · 2.43 KB
Crawler Client Logo

Advanced options

Middlewares

Middlewares are classes that implement the MiddlewareInterface. By default, middlewares from guzzle are used (except http_errors) and the client adds 3 custom middlewares:

To register your own middleware, use the CrawlerClient::withMiddlewares() method, which returns a new instance of the client. To remove the middleware use the CrawlerClient::withoutMiddlewares() method.

$client = $client
    ->withMiddlewares(new MyCustomMiddleware())
    ->withoutMiddlewares('unexpected_error');

If you would like to register some of "legacy" middlewares from Guzzle, it is possible to wrap them in an instance of the ClosureMiddleware object.

use GuzzleHttp\Middleware;
use SixtyEightPublishers\CrawlerClient\Middleware\ClosureMiddleware;

$container = [];
$client = $client->withMiddlewares(new ClosureMiddleware('history', 10, Middleware::history($container)));

Serializer

The client uses an object implementing the SerializerInterface for serialization and deserialization of request and response bodies. Serializer is implemented using jms/serializer.

The default serializer is implemented using jms/serializer and can be changed via the method CrawlerClient::withSerializer().

$client = $client->withSerializer(new MyCustomSerializer());

It is not a good idea to change the default implementation completely, the option to replace the serializer is more for emergencies, in case some error occurs and you need to fix it quickly in the application.