Downloaded pdf file using curl - issue

1.1k Views Asked by At

This is my curl code:

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

I managed to download my file. I can open it , but not in Chrome. Plus, if I am trying to open it with $pdf = new Zend_Pdf(file_get_contents($pdfFile)); I am receiving the following error: File is not a PDF

I believe the way how I am saving under a specific name and put it on a specific folder, is not correct. Any way, ideas how to approach this ?

Thank you

1

There are 1 best solutions below

0
Dave On BEST ANSWER

When you are trying to retrieve "structured" files such as a PDF you do not want the headers returned too as they will usually corrupt the file being retrieved. Change

curl_setopt($curl, CURLOPT_HEADER, 1);

to

curl_setopt($curl, CURLOPT_HEADER, 0);