I would like to copy a PDF from a URL (API) to our server with PHP.
When I call the URL in the browser, the file start to download directly, so this ain't a static PDF/URL. I think that's problem.
I've tried different functions with PHP but with no luck: file_put_contents, copy, fopen/write.
Could you please advise?
For example i've tried:
$url_label = "http://example.com/Public/downloadlabel.aspx?username=$username&password=$password&layout=label10x15&zipcode=$zipcode&shipment=$ShipmentID";
file_put_contents("label.pdf", fopen($url_label, 'r'));
The PDF-file is created in my folder, but this file is empty (0 bytes).
And with curl I hoped to pass the forced download:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_label);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$data = curl_exec($ch);
curl_close($ch);
$destination = dirname(__FILE__) . '/file.pdf';
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
curl_close($ch);
The PDF-file is created with 277 bytes, but is corrupted.
Any ideas?