简体   繁体   English

如何对来自客户端的请求做出尖锐的回应?

[英]How do I make a guzzle response into a response to a request from a client?

I have an application running on webserver A. I have a second application running on webserver B. Both webservers require a login. 我有一个在Web服务器A上运行的应用程序。我有另一个在Web服务器B上运行的应用程序。两个Web服务器都需要登录。 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). 我需要做的是请求Web服务器A传递到Web服务器B,然后将文件返回给客户端,而无需让客户端登录到Web服务器B。(换句话说,Web服务器B对客户端是不可见的,我将我从A向B的请求中照顾身份验证凭据。 The code below is built on a laravel framework, but I don't believe the answer needs to be laravel specific. 下面的代码建立在laravel框架上,但是我不认为答案需要特定于laravel。

The code works but it is only returning the HEAD information of the file to the calling client. 该代码有效,但是它只是将文件的HEAD信息返回给调用客户端。 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. 如主教所述,您可以使用Guzzle的接收sink选项来流式传输Guzzle请求的响应。

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 . 我不确定Laravel是否具有内置的流支持,但是底层的symfony httpfoundation组件可以 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 . 如果您不想使用Guzzle的接收sink选项,则还可以使用响应本身,因为它实现了PSR-7流对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM