简体   繁体   中英

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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