2017-06-16 11 views
0

私はAPI、ライブPDFファイルを入手しようとしています。そのファイルをユーザーにダウンロードしようとしています。問題を示すために、私はより基本的なものにURLを変更します。しかし私は同じ問題を抱えています。ファイルが壊れているか、類似しています。問題は簡単に説明できます。このコードをいくつかのルートにコピーするだけです。リモートURLからpdfドキュメントをダウンロード

$CurlConnect = curl_init(); 
curl_setopt($CurlConnect, CURLOPT_URL, 'http://www.pdf995.com/samples/pdf.pdf'); 
curl_setopt($CurlConnect, CURLOPT_POST, 1); 
curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1); 
$Result = curl_exec($CurlConnect); 

header('Cache-Control: public'); 
header('Content-type: application/pdf'); 
header('Content-Disposition: attachment; filename="new.pdf"'); 
header('Content-Length: '.strlen($Result)); 
echo $Result; 

答えて

1

あなたは、単にfile_get_contents()を使用することができます。

<?php 

$content = file_get_contents('http://www.pdf995.com/samples/pdf.pdf'); 

header('Cache-Control: public'); 
header('Content-type: application/pdf'); 
header('Content-Disposition: attachment; filename="new.pdf"'); 
header('Content-Length: '.strlen($content)); 

echo $content; 
+0

クロムビューアでファイルを開くことはできますか?私はまだクロムで開くときにエラーが発生しています。 PDF文書を読み込めません – anvd

+0

うん、それは私のためにうまく動作します。 – BenM

0

あなたの空のポスト文字列のようなpdf995.comのdoesntで何か。

curl_setopt($CurlConnect, CURLOPT_POST, 1); 

そして、それが正常に動作します:

は、この行を失います。