0

I'm trying to load and save a remote PDF to my server for a project, but the link has no file extension. It's a kind of secured link.

https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803

When I open the link in the browser I can download the PDF file, but saving it with my script has no success.

Is there a way to save the PDF on my server with a script? I tried following code without success:

$url ="https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803";
getFileContents($url);

function getFileContents($url)
{
// Workaround: Save temp file
$img = tempnam(sys_get_temp_dir(), 'pdf-');
$img .= '.' . pathinfo($url, PATHINFO_EXTENSION);

$fp = fopen($img, 'w+');

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$result = curl_exec($ch);
curl_close($ch);

fclose($fp);

return $result ? $img : false;
}

I found the script here : Downloading a large file using curl

Community
  • 1
  • 1
Leroy
  • 11
  • 3
  • Well, for starters you probably don't want to include your session_key in anything you're posting online. Also, did you check the related threads such as this one http://stackoverflow.com/questions/15076323/how-to-download-this-pdf-file-using-php ?? – Kaylined Sep 09 '16 at 17:51
  • No, I did saw that thread. Thanks I'll take a look into it. – Leroy Sep 09 '16 at 18:03

2 Answers2

0

To download a file (binary or not) you can use file_get_contents(). file_get_contents return the file content into a string.

//Download PDF content
$pdfContent = file_get_contents("https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=XXXXXXX-DDDD-4444-AAAA-XXXXXXX&LINK_PAGE=ITINERARYENC&TRANS_ID=AAAAAAAA-3333-5555-6666-222222222");

$fileName = "myPDF.pdf";
$fp = fopen($fileName, 'w+');
//Write content into the file
fwrite($fp, $pdfContent);
fclose($fp);

You can if necessary use options in file_get_contents to specify GET or POST method, Basic Authentication and more.

Anthony
  • 2,014
  • 2
  • 19
  • 29
0

Thanks for your reply, but your code doesn't saves the PDF im looking for. It safes a PDF with HTML content :

WWW.ENABLELOGISTICS Enable Logistics v95.4248
Welcome to Enable
Logistics Please enter your username and password to sign in.
Forgot Password?
©2016 Bright People Technologies Pty Ltd

I have no clue how to dowload the pdf to my server. Clicking the link only let's me download local.

Leroy
  • 11
  • 3