dpyu7978 2014-08-28 21:13
浏览 41
已采纳

PHP REST下载文件

I have a webservice with a function like this

$app->get('/downloadPdf', function () use($app) 
{
    $log = 'example.pdf';
    $res = $app->response();
    $res['Content-Description'] = 'File Transfer';
    $res['Content-Type'] = 'application/octet-stream';
    $res['Content-Disposition'] ='attachment; filename=' . basename($log);
    $res['Content-Transfer-Encoding'] = 'binary';
    $res['Expires'] = '0';
    $res['Cache-Control'] = 'must-revalidate';
    $res['Pragma'] = 'public';
    $res['Content-Length'] = filesize($log);
    readfile($log);
});

Testing it with Advanced Rest Client works fine..

Question is .. how do i call it from my client with all the headers etc.

To specify more. I know there are a lot of examples on how to download a specific file by inserting its url into the curlopt_url with the complete address to the file. What i want is to let the webservice decide which file to return...

Thanks

  • 写回答

2条回答 默认 最新

  • dqpfl2508589 2014-09-19 22:48
    关注

    Never got an answer .. so !!!

    This is how i made it work....

    Service Function can be seen below

    $app->post('/downloadReport', 'authenticate', function() use ($app) 
    {
    verifyRequiredParams(array('reportId'));
    
    $body = $app->request()->getBody();
    $params_str = urldecode($body);     
    $input = json_decode($params_str,true);             
    
    $report_id = $input['reportId'];    
    $db = new DbHandler();    
    $db->getReport($report_id);
    
    $path = $db->getReportPdfPath($report_id);
    
    $res = $app->response();
    $res['Content-Description'] = 'File Transfer';
    $res['Content-Type'] = 'application/octet-stream';
    $res['Content-Disposition'] ='attachment; filename=' . basename($path);
    $res['Content-Transfer-Encoding'] = 'binary';
    $res['Expires'] = '0';
    $res['Cache-Control'] = 'must-revalidate';
    $res['Pragma'] = 'public';
    $res['Content-Length'] = filesize($path);
    readfile($path);   
    
    });
    

    Called the function like this:

    public function downloadReport($api_key,$id)
    {
    
        $curl_post_data = array('reportId' => $id);
    
        $headers = array('Content-type: application/json','Authorization: '.$api_key,);
        $fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');//This is the file where we save the    information
        $curl = curl_init(DONWLOAD_REPORT);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data));
        curl_setopt($curl, CURLOPT_USERPWD, $api_key);
        $file = curl_exec($curl);
    
        if ($file === false) 
        {
            $info = curl_getinfo($curl);
            curl_close($curl);
            die('error occured during curl exec. Additioanl info: ' . var_export($info));
        }
    
        curl_close($curl);
    
        header('Content-type: ' . 'application/octet-stream');
        header('Content-Disposition: ' . 'attachment; filename=report.pdf');
        echo $file;        
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?