6

このリンクからPDFファイルをダウンロードしたい。ページから、週ごとの要約を選択します。最初のpdfをダウンロードします。

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>";


?>

PHPではダウンロードできないようです。ポップアップウィンドウを確認して自分でダウンロードできます。助言がありますか?

カールも試してみましたが、まだダウンロードできません。ファイルサイズはゼロです。

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


?>
4

3 に答える 3

11

最後に、PHPcurlと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";
}
?>
于 2013-02-26T19:35:34.730 に答える
11

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

この行を元のコードに追加しますか?

于 2013-02-25T21:08:02.077 に答える
1

また、ここで述べたように:

curlを使用してSSLから画像をダウンロードしますか?

httpsの場合、以下を追加する必要があります。

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
于 2017-05-01T14:31:42.593 に答える