doufuxi7093 2015-06-17 16:27
浏览 78
已采纳

PHP - 使用CURL验证和下载远程文件

I'm trying to use php to post variables to another php on a remote server and on the file at the remote server I want to read the file and force its download. The file on the remote server is not on a public folder, that's why I need to call the file first, so it authenticates and get the protected file.

Everything works, but I don't get any file back.

At the remote PHP I'm using standards readfile and headers to download a file.

I think maybe the problem is with the CURL setup, I'm using the standard CURL calls is there any specific header or setup I need to use when I call CURL to have the file downloaded?

This is the CURL i'm using

$request = curl_init();
curl_setopt($request, CURLOPT_URL, $postURL);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_VERBOSE, 0);
curl_setopt($request, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($request);
curl_close($request);

Where $postFields contain the variables to authenticate and confirm the file on the other server.

On the other server the php after authenticating does the normal force download:

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$newFileName.'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');

readfile($path);

Thanks!

EDIT: Hi, I believe I found the issue. The problem is that when I call through .$post on the jquery the local server php, the curl interrupts the ajax call

        $.post('local.php', $(this).serialize(), function(data){
            alert(data);
            }
        }, "json").fail(function() {
            alert('error');
        });

So I'll have to figure it out how to call through the jquery the local.php without breaking the process. Thanks for your help!

  • 写回答

1条回答 默认 最新

  • dtkl55257 2015-06-18 05:05
    关注

    Assuming you are using basic HTTP authentication on the target site - you can add logon information by using the option CURLOPT_USERPWD.

    curl_setopt($request, CURLOPT_USERPWD, "$username:$password");
    

    edit: regarding the ajax call - jQuery will execute the fail method, if the server returns an error. Furthermore - you are specifying json as data-type in your ajax-call. The download will not return json, I think.

    edit 2: I do not think that a download via $.post will be possible that way, as jQuery only knows one of xml, html, script, json, jsonp, text. You might try authenticating via your ajax request, have the ajax response return the download URL redirect the browser to that target. Or you could try using a hidden iframe for that download. Have a look at those posts (here and here) - the issue is discussed in details here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大