PHP REST下载文件 [英] PHP REST download file

查看:87
本文介绍了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);
});

使用Advanced Rest Client进行测试可以正常工作.

Testing it with Advanced Rest Client works fine..

问题是..我该如何从我的客户端调用所有标头等.

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

指定更多.我知道有很多关于如何通过将特定URL的完整地址插入curlopt_url中来下载特定文件的示例.我想要的是让网络服务决定要返回哪个文件...

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...

谢谢

推荐答案

从来没有得到答案..所以!!!

Never got an answer .. so !!!

这就是我的工作方式....

This is how i made it work....

服务功能如下所示

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

});

这样调用函数:

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

这篇关于PHP REST下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆