How to download this PDF file using php

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

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

Version 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

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

Add this line to your original code?


Finally I could download the PDF file with PHP curl and CURLOPT_REFERER setting. 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";
}
?>

Tags:

Php