我如何在对客户请求的响应中做出尖锐的响应? [英] How do I make a guzzle response into a response to a request from a client?

查看:74
本文介绍了我如何在对客户请求的响应中做出尖锐的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Web服务器A上运行的应用程序.我有另一个在Web服务器B上运行的应用程序.两个Web服务器都需要登录.我需要做的是请求Web服务器A传递到Web服务器B,然后将文件返回给客户端,而无需让客户端登录到Web服务器B.(换句话说,Web服务器B对客户端是不可见的,我将我从A向B提出的请求中,要照顾好身份验证凭证.下面的代码建立在laravel框架上,但是我认为答案不必是特定于laravel的.

该代码有效,但仅将文件的HEAD信息返回给调用客户端.不是文件本身.

任何帮助将不胜感激!

控制器:

    public function getAudioFile(Request $request)
{
    //This is the id we are looking to pull
    $uid = $request->uniqueid;
    $audioServices = new AudioServices();
    return $audioServices->getWavFile($uid);
}

服务:

    public function getWavFile(String $uniqueId)
{
    $client = new GuzzleHttp\Client(['verify' => false]);
    return $client->request('GET', $this->connectString.$uniqueId, ['auth' =>  ['username', 'password']]);
}

解决方案

中找到它的用法.. >

如果您不想使用Guzzle的sink选项,则还可以将响应本身用作实现PSR-7流对象.

I have an application running on webserver A. I have a second application running on webserver B. Both webservers require a login. What I need to do is have a request to webserver A pass through to webserver B and return a file to the client without having the client login to Webserver B. (In other words, webserver B will be invisible to the client and I will take care of the auth credentials with my request to B from A). The code below is built on a laravel framework, but I don't believe the answer needs to be laravel specific.

The code works but it is only returning the HEAD information of the file to the calling client. Not the file itself.

Any help will be greatly appreciated!

Controller:

    public function getAudioFile(Request $request)
{
    //This is the id we are looking to pull
    $uid = $request->uniqueid;
    $audioServices = new AudioServices();
    return $audioServices->getWavFile($uid);
}

Service:

    public function getWavFile(String $uniqueId)
{
    $client = new GuzzleHttp\Client(['verify' => false]);
    return $client->request('GET', $this->connectString.$uniqueId, ['auth' =>  ['username', 'password']]);
}

解决方案

As mentioned by bishop you can use sink option from Guzzle to stream the response of a Guzzle request.

You can pass that stream to a response from your controller. I'm not sure if Laravel has built-in stream support, but the underlying symfony httpfoundation components do. An example of it's usage can be found in this tutorial.

If you prefer not to use the sink option from Guzzle you can also use the response itself as that implements PSR-7 stream objects.

这篇关于我如何在对客户请求的响应中做出尖锐的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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