如何使用 php 下载此 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15076323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:31:05  来源:igfitidea点击:

How to download this PDF file using php

php

提问by supajason

I want to download a pdf file from this link. From the page, select weekly summary, I want download the first pdf.

我想从这个链接下载一个 pdf 文件。从页面中,选择每周摘要,我想下载第一个 pdf。

PDFWeekly summary - Last week: Sorted by insider

PDF每周摘要 - 上周:按内部人士排序

I'm using the following code try to download the pdf,

我正在使用以下代码尝试下载pdf,

<?php

$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";

if (file_exists($file))
{
    if (FALSE!== ($handler = fopen($file, 'r')))
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: chunked'); //changed to chunked
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        //header('Content-Length: ' . filesize($file)); //Remove

        //Send the content in chunks
        while(false !== ($chunk = fread($handler,4096)))
        {
            echo $chunk;
        }
    }
    exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";


?>

It seems I can not download using PHP. I can download by myself confirm the popup window. Any suggestions?

看来我无法使用PHP下载。我可以自己下载确认弹出窗口。有什么建议?

I tried curl also, still can not download. The file size is zero.

我也试过curl,还是不能下载。文件大小为零。

<?php

    $file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";

    $path = 'c:\download\output.pdf';

    $ch = curl_init($file);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $data = curl_exec($ch);

    curl_close($ch);

    file_put_contents($path, $data);


?>

回答by

Finally I could download the PDF file with PHP curl and CURLOPT_REFERER setting. Below is the code.

最后我可以下载带有 PHP curl 和 CURLOPT_REFERER 设置的 PDF 文件。下面是代码。

<?php

$url  = 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA';
$path = "/pdf/output.pdf";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTReportsAccessController?menukey=15.03.00&locale=en_CA');

$data = curl_exec($ch);

curl_close($ch);

$result = file_put_contents($path, $data);

if (!$result) {
    echo "error";
} else {
    echo "success";
}
?>

回答by supajason

Instead of getting your PHP server to act as a proxy why not just:

与其让您的 PHP 服务器充当代理,不如这样做:

<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header("Location: $file");
?>

Version 2

版本 2

<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf"');
readfile($file);
?>

Version 3

版本 3

curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA');

Add this line to your original code?

将此行添加到您的原始代码中?

回答by Rafa

Also, as mentioned here:

另外,正如这里提到的:

Download a image from SSL using curl?

使用 curl 从 SSL 下载图像?

for https we should add the following:

对于 https,我们应该添加以下内容:

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);