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.
use GuzzleHttp\Client;
use GuzzleHttp\Event\BeforeEvent;
$client = new Client();
// Add a listener that will echo out requests before they are sent
$client->getEmitter()->on('before', function (BeforeEvent $e) {
echo 'About to send request: ' . $e->getRequest();
});
$client->get('http://httpbin.org/get');
// Outputs the request as a string because of the event
See Event System for more information on the event system used in Guzzle.