Download the PHP package kiwilan/php-http-pool without Composer
On this page you can find all versions of the php package kiwilan/php-http-pool. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kiwilan/php-http-pool
More information about kiwilan/php-http-pool
Files in kiwilan/php-http-pool
Package php-http-pool
Short Description PHP package with easy-to-use GuzzleHttp pool wrapper to make concurrent requests.
License MIT
Homepage https://github.com/kiwilan/php-http-pool
Informations about the package php-http-pool
PHP HTTP Pool
PHP package with easy-to-use GuzzleHttp
pool wrapper, works with GuzzleHttp\Pool
and GuzzleHttp\Client
to make concurrent requests.
[!NOTE]\ I love
GuzzleHttp\Pool
, but I would to build a wrapper to make it easier to use and LaravelHttp\Pool
is cool but not flexible enough for me. SoHttpPool
allow you to send anarray
or aCollection
of requests and get aCollection<mixed, HttpPoolResponse>
of with allGuzzleHttp
features and more.Built to be more flexible that Laravel
Http
Pool, if Laravel Pool is perfect for you, keep using it.
Features
- π Works with very big pool of requests: requests chunked to avoid memory peak
- ποΈ Keep identifier of each request: easy to put response into original item (in case of
Collection
ofModel
with Laravel, for example) - π¦
HttpPoolResponse
wrapper with some features to improve DX: original ID, body, metadata... - π‘ Keep original
GuzzleHttp
response inHttpPoolResponse
: you're in home - π¨ Allow handle memory peak: if you have a lot of requests
- ποΈ Works with simple arrays, with associative arrays, with array of objects, with Laravel
Collection
: just define where to get identifier and URL - π¬ Optional console output: you can disable it if you don't want to see progress
- π Works with any PHP frameworks,
Illuminate\Support\Collection
is a dependency but you can use it without Laravel,toArray()
method is available after pool execution if you don't want to useCollection
Installation
You can install the package via composer:
Usage
Input
When you want to use HttpPool
, you have to pass an input, it could be: a simple array, an associative array, a Laravel Collection
or an array of objects.
With simple array
Associative array
[!WARNING]\ Identifier and URL have to not be nested.
Laravel models
Take a Laravel model collection and send requests with HttpPool
. Here Book
is a Laravel model, we assume that Book
has an id
attribute and a google_book_api
attribute.
Array of objects
Here we take an array of objects, we assume that each object has an uuid
attribute and an url
attribute. You can just define getters like getUuid()
and getUrl()
or you can use public
attributes, it's up to you.
[!WARNING]\ If attributes are
private
orprotected
, you have to define getters with logic names:getUuid()
andgetUrl()
. You can useuuid()
andurl()
too as getters. But here, if you create a gettergetBookUuid()
, it will not work.
Execution
To execute pool, you can use execute()
method.
execute()
method returns a HttpPoolFullfilled
object. You can get pool with getPool()
method.
In HttpPoolFullfilled
object, you can get responses and more features. All methods getResponses()
,getFullfilled()
, getRejected()
are Illuminate\Support\Collection
of HttpPoolResponse
.
Errors
To handle errors, you can just use HttpPool::make()
method and errors will throw exceptions. But if you want to prevent errors, you can use throwErrors
param.
All errors can be found in getErrors()
method, after pool execution.
Response
After pool execution, you can get responses with getResponses()
method. It returns a Collection
of HttpPoolResponse
.
[!NOTE]\ The first item of
getResponses
could not be the first request you sent. It depends of the response time of each request. But you can retrieve the original request withgetMetadata()->getRequest()
method, the best way to find parent is to define an ID, that you could retrieve it withgetId()
method.
Metadata
HttpPoolResponse
has a HttpPoolResponseMetadata
attribute, it contains some useful data. Here $first
is a HttpPoolResponse
.
Body
HttpPoolResponseBody
is a wrapper of GuzzleHttp\Psr7\Stream
with some useful methods. Here $first
is a HttpPoolResponse
.
Advanced
You can use some advanced options to customize your pool.
Use URL as identifier to replace ID.
Enable console output.
Memory peak
Handle memory peak is optional, but if you have a lot of requests, you can use allowMemoryPeak
to avoid memory peak. New memory peak will be set inside execute()
method.
Memory peak is set to 2G
by default, you can change it with second param.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
- Guzzle for the awesome HTTP client
- Laravel for
Illuminate\Support\Collection
- Symfony for
symfony/console
- Spatie for the package skeleton
- Ewilan Rivière
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of php-http-pool with dependencies
guzzlehttp/guzzle Version ^7.7
illuminate/support Version ^10 || ^11.0
symfony/console Version ^6 || ^7