Guzzle并发去请求图片并保存到本地
现在有个小需求,需要用Guzzle并发去请求图片并保存到本地,并发的数量是不固定的,下面测试代码,请教大家指点一下!
public function onGoodsSynchronizationEvent(GoodsSynchronizationEvent $goodsSynchronizationEvent,Client $client){
$requests = function (array $imageInfo) {
foreach ($imageInfo as $image){
yield new Request('GET', $image['url']);
}
};
$pool = new Pool($client, $requests($goodsSynchronizationEvent->imageInfo), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {
// this is delivered each successful response
},
'rejected' => function ($reason, $index) {
// this is delivered each failed request
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
}
我只了解请求单个可以这样使用
$client->get($rul,['save_to'=>$fileName);
推荐文章: