It is assumed that when initializing $client
with new GuzzleHttp\Client()
if you do not pass a handler
explicit%, the client instance chooses the best handler
for you.
When you do not have curl
or php-curl
installed, the default handler is StreamHandler .
Think of an endpoint endpoint.php
that receives a parameter as a parameter and responds after that time
<?php
//endpoint.php
sleep($_GET['wait']);
echo 'listo';
According to the Handlers documentation , if you explicitly used the StreamHandler
$handler = new \GuzzleHttp\Handler\StreamHandler();
$stack = \GuzzleHttp\HandlerStack::create($handler);
$client = new \GuzzleHttp\Client(['handler' => $stack]);
$tini = time();
$promises = [];
$promises['demora3'] = $client->getAsync('http://sitio.com/endpoint.php?wait=3');
$promises['demora2'] = $client->getAsync('http://sitio.com/endpoint.php?wait=2');
$promises['demora1'] = $client->getAsync('http://sitio.com/endpoint.php?wait=1');
$promises['demora3']->then(function ($res) use ($tini) {
echo '<br> demora3: demoró ' . (time() - $tini) . ' segundos ';
});
$promises['demora2']->then(function ($res) use ($tini) {
echo '<br> demora2: demoró ' . (time() - $tini) . ' segundos ';
});
$promises['demora1']->then(function ($res) use ($tini) {
echo '<br> demora1: demoró ' . (time() - $tini) . ' segundos ';
});
$results = \GuzzleHttp\Promise\settle($promises)->wait();
Requests would be solved in a synchronous way and the result would be:
demora3: demoró 6 segundos
demora2: demoró 6 segundos
demora1: demoró 6 segundos
Because it took 1 + 2 + 3 seconds in total.
To achieve what you want, you would have to have curl
installed on your system, and the php-curl
extension, and for more security, explicitly tell you that you want to use CurlMultiHandler .
$handler = new \GuzzleHttp\Handler\CurlMultiHandler();
$stack = \GuzzleHttp\HandlerStack::create($handler);
$client = new \GuzzleHttp\Client(['handler' => $stack]);
$tini = time();
$promises = [];
$promises['demora3'] = $client->getAsync('http://sitio.com/endpoint.php?wait=3');
$promises['demora2'] = $client->getAsync('http://sitio.com/endpoint.php?wait=2');
$promises['demora1'] = $client->getAsync('http://sitio.com/endpoint.php?wait=1');
$promises['demora3']->then(function ($res) use ($tini) {
echo '<br> demora3: demoró ' . (time() - $tini) . ' segundos ';
});
$promises['demora2']->then(function ($res) use ($tini) {
echo '<br> demora2: demoró ' . (time() - $tini) . ' segundos ';
});
$promises['demora1']->then(function ($res) use ($tini) {
echo '<br> demora1: demoró ' . (time() - $tini) . ' segundos ';
});
$results = \GuzzleHttp\Promise\settle($promises)->wait();
And you should get:
demora1: demoró 1 segundos
demora2: demoró 2 segundos
demora3: demoró 3 segundos