Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP REST下载文件_Php_File_Rest - Fatal编程技术网

PHP REST下载文件

PHP REST下载文件,php,file,rest,Php,File,Rest,我有一个Web服务,它的功能如下 $app->get('/downloadPdf', function () use($app) { $log = 'example.pdf'; $res = $app->response(); $res['Content-Description'] = 'File Transfer'; $res['Content-Type'] = 'application/octet-stream'; $res['Conte

我有一个Web服务,它的功能如下

$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服务决定返回哪个文件


谢谢,我一直没有得到答案。。所以

这就是我让它工作的方式

服务功能如下所示

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

我和你有同样的问题, 我正在使用这段代码,它可以从webservice返回一个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;
参考资料:


您的日志文件是php脚本吗
readfile
不执行代码,它只是将字节流输出。希望它只是一个碰巧有.php扩展名的文件。。我打算用我的服务下载pdf文件。你的客户是什么?页面上的javascript?服务器上的php?ios应用程序?不知道你在问什么“我如何从我的客户那里使用所有的标题来调用它?”Web服务看起来不需要任何信息。。。只需点击url/downloadPdf和一个get请求。该请求是由我的服务处理程序发出的,该处理程序是服务器上的php。我仍然对此感到困惑。。。如前所述,我已经编写了处理请求的函数。。但我仍然不明白如何通过php从客户端代码调用它。。非常感谢您的帮助:)