【问题标题】:Trying to download mp3 file via API GET response using guzzle client in laravel尝试使用 laravel 中的 guzzle 客户端通过 API GET 响应下载 mp3 文件
【发布时间】:2021-07-19 17:31:32
【问题描述】:

我尝试了几种通过 API 获取请求下载 mp3 文件的方法。我感觉好像我很接近但似乎无法下载。

我的网址返回一个二进制 mp3 文件。

这是我在随 get 请求返回的响应标头数组中得到的部分内容。希望这会有所帮助。

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

这是我在 Laravel 中使用 Guzzle 客户端的代码示例。尝试 Laravel 的下载方法,但我相信我需要从 Content-Disposition 获取实际文件。非常感谢任何帮助。谢谢。

    $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;
    }

【问题讨论】:

  • 那么到底出了什么问题?不清楚
  • 我无法在浏览器中下载文件。我确实添加了这段 javascript,但无法让它工作。正在尝试粘贴脚本。
  • `console.log("data", response.data); const url = window.URL.createObjectURL(new Blob([response.data]));常量链接 = document.createElement('a');链接.href = 网址; link.setAttribute('下载', 'file.mp3'); document.body.appendChild(link);链接.click();`
  • 您是否尝试在浏览器中通过 Ajax 下载文件?
  • “但我认为我需要从 Content-Disposition 中获取实际文件” - 除非您需要从那里提取文件名,否则我不明白为什么什么是必要的。 Laravel 的 download 方法能否处理完整的 Guzzle 响应对象,或者您可能只需要使用 $response->getBody() 获取正文内容?

标签: php laravel download mp3 guzzle


【解决方案1】:

#注意:这不是经过测试的答案,我只是提供了一个示例来遵循上面的 cmets

<?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;
    }

【讨论】:

  • 非常感谢。看看。
猜你喜欢
  • 1970-01-01
  • 2020-11-20
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多