2014-08-28 102 views
1

我有一個Web服務,像這樣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); 
}); 

測試它與高級REST客戶端的功能工作正常..

問題是..我怎樣把它從我的客戶端所有標題等。

指定更多。我知道有很多關於如何下載特定文件的例子,方法是將它的url插入curlopt_url並附上文件的完整地址。我要的是讓web服務決定要返回的文件...

感謝

+0

您的日誌文件是一個PHP腳本? 'readfile'不執行代碼,它只是流式輸出字節。希望它只是一個恰好有一個.php擴展名的文件。 – 2014-08-28 21:22:59

+0

它只是一個測試文件..我打算用我的服務下載pdf文件.. – DTH 2014-08-28 21:25:36

+0

你的客戶是什麼?在頁面上的JavaScript?在服務器上的PHP? iOS應用程序?不知道你在問什麼「我怎麼從所有頭文件的客戶端調用它?」 webservice看起來並不需要任何信息...只需通過獲取請求來打開url/downloadPdf。 – 2014-08-28 23:48:45

回答

3

沒有得到回答..所以!

這是我如何做它的工作....

服務功能可以看到下面

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

我有同樣的問題你, 我使用這個代碼,它的工作原理,從web服務返回一個PDF文件。

 $api = new RestClient(array(
       'base_url' => 'http://another-webservice.com/', 
       'headers' => array(
           'X-Token' => $res->headers->x_token, 
           'Accept' => 'application/pdf', 
          ), 
      )); 

     $result = $api->execute("reports/report",'GET', $params); 

     $filename = 'Report.pdf'; 
     header('Content-type: application/pdf'); 
     header('Content-Disposition: inline; filename="' . $filename . '"'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Accept-Ranges: bytes'); 
     echo $result->response; 

參考文獻:How to display PDF in Browser