简体   繁体   中英

Trying to download mp3 file via API GET response using guzzle client in laravel

I've tried several methods to download an mp3 file via API get request. I feel like as if I'm close but just can't seem to get the download.

My URL returns a binary mp3 file.

This is a portion of what I'm getting in my response header array returning with the get request. Hope this is helpful.

"Content-Disposition" => array:1 [
      0 => "attachment; filename=RE3e327a2615b93f528fee111da9b60e17.mp3; filename*=UTF-8''sample.mp3"
    ]

Here is a sample of my code using the Guzzle client in Laravel. Trying Laravel's download method but I believe I need to get the actual file from the Content-Disposition. Much appreciated for any help. Thanks.

    $client = new Client();

    try {
        $url = 'http://getmp3website.net/recording/sample.mp3';


        $response = $client->request('GET', $url,
         [
            'headers' => [
                'Authorization' => 'bearer ' . env("AUTH_TOKEN"),
                'Content-Type' => 'audio/mp3',
            ],
        ]);

        return response()->download($response);

    } catch (Exception $ex) {
        return $ex;
    }

#Note: this is not a tested answer, I have just provided an example to follow the comments above

<?php

    $client = new Client();

    try {
        $url = 'http://getmp3website.net/recording/sample.mp3';
        
        $resource = \GuzzleHttp\Psr7\Utils::tryFopen('/path/to/file', 'w');
        //or you can use $myFile = fopen('/path/to/file', 'w') or die('not working');
        $stream = \GuzzleHttp\Psr7\Utils::streamFor($resource);
        $client->request('GET', $url , [
                'save_to' => $stream,
                'headers' => [
                    'Authorization' => 'bearer ' . env("AUTH_TOKEN"),
                    'Content-Type' => 'audio/mp3',
                ],
            ]
        );
        
        /**
         *  // As `save_to` is deprecated(guzzle wants us to download files as stream I guess), you can use sink as well, sink will automatically stream files for you
            $resource = \GuzzleHttp\Psr7\Utils::tryFopen('/path/to/file', 'w');
            $client->request('GET', $url, ['sink' => $resource]);
        */

        return response()->download($pathsavedfile);

    } catch(\GuzzleHttp\Exception\RequestException $e){
               // you can catch here 400 response errors and 500 response errors
               // You can either use logs here 
               $error['error'] = $e->getMessage();
               $error['request'] = $e->getRequest();
               if($e->hasResponse()){
                   if ($e->getResponse()->getStatusCode() == '400'){
                       $error['response'] = $e->getResponse(); 
                   }
               }
               Log::info('Error occurred in request.', ['error' => $error]);
        }catch (Exception $ex) {
        return $ex;
    }

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