Guzzle 获取文件并转发它

Guzzle get file and forward it

我有一个获取文件的网络服务,returns它给用户(基于 Symfony)。 自从我用 curl 来做这个。

我刚找到 guzzlehttp,它看起来很棒。但是,我不知道如何在不将下载的文件(xml 或 txt)保存到本地文件的情况下使用 guzzle 执行此操作,从文件系统读取它并将其返回给用户。我想在不保存到文件系统的情况下执行此操作。

public function streamAction()
{
     $response = $client->request(
        'GET', 'http://httpbin.org/stream-bytes/1024', ['stream' => true]
     );

     $body = $response->getBody();

     $response = new StreamedResponse(function() use ($body) {
         while (!$body->eof()) {
             echo $body->read(1024);
         }
     });

     $response->headers->set('Content-Type', 'text/xml');

     return $response;
}
$response = $client->request('GET', 'http://example.com/file', ['stream' => true]);
        $stream = $response->getBody();
        $content = new StreamedResponse(function () use ($stream) {
            /** @var StreamInterface $stream */
            while ($binary = $stream->read(1024)) {
                echo $binary;
                ob_flush();
                flush();
            }
        }, $response->getStatusCode(), [
            'Content-Type' => $response->getHeaderLine('Content-Type'),
            'Content-Length' => $response->getHeaderLine('Content-Length'),
            'Content-Disposition' => $response->getHeaderLine('Content-Disposition')
        ]);

        return $this->renderResponse($content->send());