使用AJAX和Guzzle 6的并行请求

时间:2019-08-26 19:03:57

标签: php jquery ajax guzzle guzzle6

我正在尝试对使用Guzzle调用数据的PHP页面执行并行并行AJAX请求。但是,每个AJAX请求都在等待上一个请求完成后再继续。

如何执行并行请求?

我已经尝试了Guzzle Pool和Batch,但这在我的用例中并没有真正的作用。

编辑:这是我现在拥有的代码:

// Create pool of requests
$generator = function (array $urls) {
    global $client;
    global $apiClientKey;
    global $apiSecretKey;
    foreach ($urls as $url) {
        yield function() use ($client, $url, $apiClientKey, $apiSecretKey) {
	    return $client->getAsync($url, ['auth' => [$apiClientKey, $apiSecretKey]]);
        };
    }
};
	
// Process pool of requests
$pool = new Pool($client, $generator($urls), [
    'concurrency' => 7,
    'fulfilled' => function ($response) {
	    return $response->getBody();
    },
    'rejected' => function ($exception) {
	    $exceptionMessage = explode("\n", $exception->getMessage());
        print_r($exceptionMessage[1]);
    },
]);
	
// Initiate the transfers and create a promise
$promise = $pool->promise();
	
// Force the pool of requests to complete.
$promise->wait();

0 个答案:

没有答案