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;
  3. $client = new Client();
  4. // Add a listener that will echo out requests before they are sent
  5. $client->getEmitter()->on('before', function (BeforeEvent $e) {
  6. echo 'About to send request: ' . $e->getRequest();
  7. });
  8. $client->get('http://httpbin.org/get');
  9. // Outputs the request as a string because of the event

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