Pull request by @WendellAdriel
$responses = Http::batch(fn (Batch $batch) => [
$batch->get('http://localhost/first'),
$batch->get('http://localhost/second'),
$batch->get('http://localhost/third'),
])->before(function (Batch $batch) {
// This runs before the first HTTP request is executed.
})->progress(function (Batch $batch, int|string $key, Response $response) {
// This runs after each successful HTTP request from the Batch.
})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) {
// This runs after each failed HTTP request from the Batch.
})->then(function (Batch $batch, array $results) {
// This runs ONLY IF all the HTTP requests from the Batch are successful and the batch is not cancelled.
})->finally(function (Batch $batch, array $results) {
// This runs after all the HTTP requests from the Batch finish and the batch is not cancelled.
})->send();
The new Http::batch method allows you to hook into the lifecycle of each HTTP call when making multiple requests in parallel.