我正在尝试通过API,实时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;
答案 0 :(得分: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;
答案 1 :(得分:0)
pdf995.com上的某些内容并不喜欢你的空帖子。
失去这一行:
curl_setopt($CurlConnect, CURLOPT_POST, 1);
它运作正常。