2017-06-16 151 views
0

我想通過一個API,一個活的pdf文件,然後將該文件下載到我的用戶。爲了演示這個問題,我將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

你可以用chrome查看器打開文件嗎?在Chrome中打開時我仍然遇到錯誤。無法加載PDF文檔 – anvd

+0

是的,它適合我。 – BenM

0

在pdf995.com犯規喜歡你的空字符串後。

失去這一行:

curl_setopt($CurlConnect, CURLOPT_POST, 1); 

,它工作正常。