狂吃获取文件并将其转发 [英] Guzzle get file and forward it

查看:79
本文介绍了狂吃获取文件并将其转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务,该服务获取文件并将其返回给用户(基于Symfony)。
自从我用curl做到这一点。

I have a web-service that gets a file and returns it to the user (based on Symfony). Ever since I used curl to do this.

我刚发现guzzlehttp,它看起来很棒。但是,我不知道如何在不将下载的文件(xml或txt)保存到本地文件,从文件系统中读取并将其返回给用户的情况下使用狂吃。我想这样做而不保存到文件系统。

I just found guzzlehttp and it seems great. However, I do not know how to do this with guzzle without saving the downloaded file (xml or txt) to a local file, read it from the file system a returning it to the user. I want to do this without saving to the file system.

推荐答案

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;
}

这篇关于狂吃获取文件并将其转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆