使用 curl 下载 pdf 文件 - 问题
Downloaded pdf file using curl - issue
这是我的 curl 代码:
$fp = fopen('path/to/file/'. $id . '.pdf', 'w+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'myurl');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, '');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_exec($curl);
我成功下载了我的文件。我可以打开它,但不能在 Chrome 中打开。另外,如果我尝试使用 $pdf = new Zend_Pdf(file_get_contents($pdfFile));
打开它,我会收到以下错误:File is not a PDF
我认为我以特定名称保存并将其放在特定文件夹中的方式不正确。任何方式,想法如何处理这个?
谢谢
当您尝试检索 "structured" 文件(如 PDF)时,您不希望 headers 也返回,因为它们通常会损坏正在检索的文件。变化
curl_setopt($curl, CURLOPT_HEADER, 1);
至
curl_setopt($curl, CURLOPT_HEADER, 0);
这是我的 curl 代码:
$fp = fopen('path/to/file/'. $id . '.pdf', 'w+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'myurl');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, '');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_exec($curl);
我成功下载了我的文件。我可以打开它,但不能在 Chrome 中打开。另外,如果我尝试使用 $pdf = new Zend_Pdf(file_get_contents($pdfFile));
打开它,我会收到以下错误:File is not a PDF
我认为我以特定名称保存并将其放在特定文件夹中的方式不正确。任何方式,想法如何处理这个?
谢谢
当您尝试检索 "structured" 文件(如 PDF)时,您不希望 headers 也返回,因为它们通常会损坏正在检索的文件。变化
curl_setopt($curl, CURLOPT_HEADER, 1);
至
curl_setopt($curl, CURLOPT_HEADER, 0);