How to download this pdf file using php

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

PDF Weekly Summary - Last Week: Insider Sorting

I am using the following code, try downloading 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>";


?>

I can't seem to download using PHP. I can download it myself, confirming the popup. Any suggestions?

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

<?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);


?>
+5
source share
3 answers

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

<?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";
}
?>
+9
source

, PHP- -, :

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

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);
?>

3

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

?

+10

, :

SSL curl?

for https we must add the following:

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+1
source

All Articles