Guzzlehttp - 在每个请求完成后立即调用相同回调的正确方法

时间:2016-05-13 08:42:18

标签: php guzzle guzzle6

在使用Guzzle进行异步请求时,我希望在每个请求完成后立即得到相同的回调。

以下代码有效,但我认为这可能没有意义。

Guzzle是否提供了Promise\settle($promises)->then($callback)->wait()之类的方法,但每次请求都已完成?

<?php
require("vendor/autoload.php");

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client(['base_uri' => 'http://httpbin.org/']);

// I want got callback immediately when each request done.
$callback = function($response) {
    echo $response->getBody();
};

$promises = [
    $client->getAsync('/delay/8')->then($callback),
    $client->getAsync('/delay/4')->then($callback),
    $client->getAsync('/delay/2')->then($callback),
    $client->getAsync('/delay/1')->then($callback),
];
Promise\settle($promises)->wait();

0 个答案:

没有答案