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

对着背影说爱祢 提交于 2019-12-11 19:46:05

问题


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']]);
}

回答1:


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.



来源:https://stackoverflow.com/questions/57255662/how-do-i-make-a-guzzle-response-into-a-response-to-a-request-from-a-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!