webman 控制器返回远程服务器的文件流,参考了帖子
https://www.workerman.net/q/13212
public function streamFile()
{
$http = new \Workerman\Http\Client();
$url ='https://xxxx.cn/uploads/default/2024-10/09/670629b1d8f8a.jpeg';
$connection = \request()->connection;
// $http = new Client();
$http->request($url, [
'method' => 'GET',
'progress' => function($buffer) use ($connection) {
print_r($buffer);
$connection->send(new \Workerman\Protocols\Http\Chunk($buffer));
},
'success' => function($response) use ($connection) {
$connection->send(new \Workerman\Protocols\Http\Chunk('')); // 发送空的的chunk代表response结束
},
]);
$downloadName = uniqid().'.jpeg';
return response()->withHeaders([
"Content-Type" => "application/jpeg",
"Transfer-Encoding" => "chunked",
"Content-Disposition"=>"attachment;filename=\"$downloadName\""
]);
}
请求后下载的是一个空文件,
progress 回调内也是可以读取到文件的二进制内容的,
win10 调试
PHP 8.0.2
"workerman/http-client": "^2.2",
"workerman/webman-framework": "^1.5",
本地调试,没有使用nginx 作为反向代理
workerman版本贴下
4.1.5
有点旧了,升级下
升级到4.2.0还是不行
Content-Type ?
这个删除了,还是不行,不过我重新安装了一下 composer.json,代码就正常了