Php LaravelAPI返回文件,客户端必须显示下载对话框

Php LaravelAPI返回文件,客户端必须显示下载对话框,php,curl,laravel-5,download,Php,Curl,Laravel 5,Download,我正在开发一个Laravel 5.3 API Brige,用于将一些文件下载到各种系统。我想要达到的目标: 用户点击下载按钮 PHPWeb向API发出一个curlpost请求 Api响应可以是文件或404 HTTP代码 客户端浏览器显示文件下载对话框 APi方法: $reportService = new ReportService($request->get('vRefGMS')); $reportData = $reportService->handle();

我正在开发一个Laravel 5.3 API Brige,用于将一些文件下载到各种系统。我想要达到的目标:

  • 用户点击下载按钮
  • PHPWeb向API发出一个curlpost请求
  • Api响应可以是文件或404 HTTP代码
  • 客户端浏览器显示文件下载对话框
  • APi方法:

        $reportService = new ReportService($request->get('vRefGMS'));
        $reportData = $reportService->handle();
        if ($reportData) {
            $serverService = new NetServerService($reportData);
            $csvFile = $serverService->handle();
            if ($csvFile != null) {
                return response()->file($csvFile);
            } else {
                return abort(404);
            }
        } else {
            return abort(404);
        }
    
    现在,我将向您展示我尝试过的代码

    web中的PHP代码可供下载:

        $uri = $this->getEndpointShow($this->reportCode, SELF::ENDPOINT_REPORT_DOWNLOAD);
        $file = $this->apiConnection->downloadReport($uri, $this->reportCode);
        if ($file) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . basename($file) . '"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            flush();
            readfile($file);
        } else {
            echo "alert('Can´t find a report file')";
        }
    
    以及下载报告方法:

    public function downloadReport($uri, $reportCode)
    {
        if (!$reportCode) {
            throw new InvalidArgumentException("No report Code");
        }
        $cURLConnection = curl_init();
        curl_setopt($cURLConnection, CURLOPT_URL, self::BASE_API_URL . $uri);
        curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cURLConnection, CURLOPT_POST, TRUE);
        curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, "vRefGMS=$reportCode");
        curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, [
            'Authorization: Bearer ' . self::API_ACCESS_TOKEN
        ]);
    
        $response = curl_exec($cURLConnection);
    
        if ($response === false) {
            throw new Exception(curl_error($cURLConnection), curl_errno($cURLConnection));
        }
    
        curl_close($cURLConnection);
    
        return $response;
    }
    
    正如您在下载的PHP代码中所看到的,我有一个
    $file
    var,它总是为空
    $file=''
    。我也使用
    return response()->download($csvFile)
    尝试了api,得到了相同的结果

    可能是我误解了概念,但我无法实现文件下载。如何获取此文件

    编辑:

    api正在返回一个文件,因为如果我在cli上向api发出一个cURL请求,这就是响应:

    curl http://localhost:8080/test
    
    
    StatusCode        : 200
    StatusDescription : OK
    Content           : 1;!;1;1;
    RawContent        : HTTP/1.1 200 OK
                        Accept-Ranges: bytes
                        Content-Length: 8
                        Cache-Control: public
                        Date: Mon, 24 Feb 2020 01:59:37 GMT
                        Last-Modified: Mon, 24 Feb 2020 12:11:04 GMT
                        Set-Cookie: XSRF-TOKEN=eyJpdiI6ImdFb...
    Forms             : {}
    Headers           : {[Accept-Ranges, bytes], [Content-Length, 8], [Cache-Control, public], [Date, Mon, 24 Feb 2020
                        01:59:37 GMT]...}
    Images            : {}
    InputFields       : {}
    Links             : {}
    ParsedHtml        : mshtml.HTMLDocumentClass
    RawContentLength  : 8
    

    那么,在显示空白文件的web代码中,我做错了什么?

    那么,您现在的实际问题是什么?这似乎是几个问题挤在一起的结果。如果您从其他地方调用API,那么您的API是否首先返回正确的内容?“可能是我误解了概念”-这可能也是,是的。如果您的API调用应该返回实际的文件内容(?),那么对该值使用
    basename
    filesize
    readfile
    当然没有意义。所有这些函数都以文件系统路径为参数。我的API返回文件(如果我从浏览器调用它试图下载文件的URL)。但是在web代码中,$file var在调用basename、filesize或readfile之前是空的,所以每行代码都不会执行,只执行
    警报()
    如果在浏览器中调用它,那么可能是在谈论GET请求。在cURL代码中,您试图发出POST请求。当然,我只是在Get端点上复制粘贴代码,以便在浏览器中进行测试。相同的代码,带有harcoded参数。您的CLI cURL请求也只是一个GET请求。