如何解决使用GuzzlePool下载多个文件不会写入任何文件
我需要使用并发请求通过Guzzle下载多个文件,所以我选择使用Pool。
基本上,我编写了一个函数,该函数接收URL列表(这些路径是我需要下载的远程文件)和路径(即我要保存文件的目录)。 该函数应将要向其发送请求的每个文件保存在提供的目录中,但该目录保留为空,并且其中没有写入的文件。
我在做什么错了?
这是我的函数,有一些回显用于调试目的:
function async_multiple_files_download($files_url_list,$path) {
$client = new \GuzzleHttp\Client();
$requests = array();
for ($i = 0; $i < sizeof($files_url_list); $i++) {
$file_name = basename($files_url_list[$i]);
$request_destination_file_path = $path . DIRECTORY_SEPARATOR . $file_name;
$requests[$i] = new GuzzleHttp\Psr7\Request('GET',$files_url_list[$i],['sink' => $request_destination_file_path]);
echo "Downloading " . basename($files_url_list[$i]) . "<br>from($files_url_list[$i])<br>to $request_destination_file_path" . "<br><br><br>";
}
$pool = new \GuzzleHttp\Pool($client,$requests,[
'concurrency' => 10,'fulfilled' => function (\Psr\Http\Message\ResponseInterface $response,$index) {
echo 'success: '.$response->getStatusCode()."<br>";
},'rejected' => function ($reason,$index) {
echo 'failed: '.$reason."<br>";
},]);
$promise = $pool->promise();
$promise->wait();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。