Event Subscribers

Requests emit lifecycle events when they are transferred. A client object has a GuzzleHttp\Common\EventEmitter object that can be used to add event listeners and event subscribers to all requests created by the client.

Important

Every event listener or subscriber added to a client will be added to every request created by the client.

1. use GuzzleHttp\Client;
2. use GuzzleHttp\Event\BeforeEvent;

4. $client = new Client();

6. // Add a listener that will echo out requests before they are sent
7. $client->getEmitter()->on('before', function (BeforeEvent $e) {
8. echo 'About to send request: ' . $e->getRequest();
9. });

11. $client->get('http://httpbin.org/get');
12. // Outputs the request as a string because of the event

See Event System for more information on the event system used in Guzzle.