我正在使用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有什么想法吗?
发布于 2018-09-18 22:43:23
首先,您不需要ReactPHP就可以将并行HTTP请求与Guzzle一起使用。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 (我是开发人员,请随时提出问题)。
https://stackoverflow.com/questions/52376076
复制相似问题