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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题