Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP REST download file

Tags:

rest

file

php

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

like image 894
DTH Avatar asked Aug 28 '14 21:08

DTH


People also ask

How do I download a file from REST API?

Enter the URL of the REST Service (f.e. http://localhost:8080/rest-file-manager/resr/file/upload ) Select POST as method. Select form-data in the Body. Enter as key “attachment” of Type File.

How can I download a file from PHP?

Initialize a file URL to the variable. Create cURL session. Declare a variable and store the directory name where the downloaded file will save. Use the basename() function to return the file basename if the file path is provided as a parameter.


1 Answers

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;        
}
like image 135
DTH Avatar answered Sep 30 '22 19:09

DTH