PHP REST下载文件

潜孔

我有一个具有这样功能的网络服务

$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进行测试可以正常工作。

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

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

谢谢

潜孔

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

这就是我的工作方式。

服务功能如下

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP CURL下载文件

来自分类Dev

php curl 下载文件

来自分类Dev

用php和polymer下载文件

来自分类Dev

无法使用PHP强制下载文件

来自分类Dev

如何使用php下载文件?

来自分类Dev

使用php压缩并下载文件

来自分类Dev

通过PHP表格下载文件

来自分类Dev

如何用php下载文件?

来自分类Dev

用ajax和php下载文件

来自分类Dev

如何使用php下载文件?

来自分类Dev

php - 使用 mysql 下载文件

来自分类Dev

PHP检查下载链接而不下载文件

来自分类Dev

PHP检查下载链接,而无需下载文件

来自分类Dev

在使用 php 下载文件之前检查文件

来自分类Dev

在PHP中同时下载文件列表

来自分类Dev

使用PHP从Amazon s3压缩并下载文件

来自分类Dev

下载文件并使用PHP本地保存

来自分类Dev

使用PHP在文档根目录之外下载文件

来自分类Dev

PHP-下载文件并保存时间戳?

来自分类Dev

通过网站从PHP服务器下载文件

来自分类Dev

php从其他服务器下载文件

来自分类Dev

jQuery将变量发布到php并下载文件

来自分类Dev

从另一个php脚本下载文件

来自分类Dev

如何从服务器或本地Xampp下载文件?的PHP

来自分类Dev

使用PHP的Google Drive API下载文件

来自分类Dev

从Azure存储Blob下载文件的简单PHP cURL方法

来自分类Dev

在php中下载文件时防止多个连接

来自分类Dev

无法使用PHP查看已下载文件中的数据

来自分类Dev

通过化学中的PHP从Alfresco下载文件