如何一次发送多个请求ReactPHP?

问题描述 投票:0回答:1

我正在使用guzzle发送一些请求:

$response = $this->client->request(new SomeObject());

使用下面的课程

...
public function request(Request $request)
{
    return $this->requestAsync($request)->wait();
}

// Here I'm using Guzzle Async, but I can remove that is needed
public function requestAsync(Request $request)
{
    $promise = $this->http->requestAsync($request->getMethod(), $request->getUri());

    $promise = $promise->then(
        function (ResponseInterface $response) use ($request) {
            return $response;
        }
    );

    return $promise;
}
...

我想使用ReactPHP在foreach循环中一次发送多个请求:

$requests = [];
foreach ($data as $value) {
    $requests[] = $this->client->request(new SomeObject());
}

// pass $requests to ReactPHP here and wait on the response

有任何想法吗?

php asynchronous guzzle reactphp
1个回答
0
投票

首先,您不需要ReactPHP来使用Guzzle的并行HTTP请求。 Guzzle本身提供此功能(如果您使用cURL处理程序,这是默认值)。

例如:

$promises = [];
foreach ($data as $value) {
    $promises[] = $guzzleClient->getAsync(/* some URL */);
}

// Combine all promises
$combinedPromise = \GuzzleHttp\Promise\all($promises)

// And wait for them to finish (all requests are executed in parallel)
$responses = $combinedPromise->wait();

如果您仍然希望使用Guzzle和ReactPHP事件循环,那么,遗憾的是,没有直接的解决方案。你看看https://github.com/productsupcom/guzzle-react-bridge(我是开发人员,所以随时提问)。

© www.soinside.com 2019 - 2024. All rights reserved.