dougyfrmja's avatar

Download file with Guzzle

I have an api endpoint that returns a pdf file. How can I use Guzzle to download the file to my user's browser?

public function invoicepdf($invoice_num){
    
    $client = new Client();
    $request = new Request('GET', 'http://mysite/getinvoicepdf/'.$invoice_num);

    try {
        $response = $client->send($request, [
            'auth' => ['username', 'password'],
            'query' => [
                'company_id' => $this->company_id
                ]
       ]);
    } catch (RequestException $e) {
        return false;
    }

    return true;
}
0 likes
3 replies
Snapey's avatar

concerns me that apparently you don't have any security's around who can access the invoices ?

dougyfrmja's avatar

Hey guys, @studlycaps suggestion did work! There was a firewall issue that prevented it from working at first. Then i realised that i cant use @studlycaps suggestion within a trait method. So i moved it to the controller using the trait and it worked.

@snapey authentication is required on the web client from the user and when they try to access the invoice there is a separate authentication from the web client backend controller to the invoice api.

Please or to participate in this conversation.