Downloading a File from Secure website (https) using CURL


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <php
  2. /**
  3. * Copy File from HTTPS/SSL location
  4. *
  5. * @param string $FromLocation
  6. * @param string $ToLocation
  7. * @return boolean
  8. */
  9. function copySecureFile($FromLocation,$ToLocation,$VerifyPeer=false,$VerifyHost=true)
  10. {
  11. // Initialize CURL with providing full https URL of the file location
  12. $Channel = curl_init($FromLocation);
  13.  
  14. // Open file handle at the location you want to copy the file: destination path at local drive
  15. $File = fopen ($ToLocation, "w");
  16.  
  17. // Set CURL options
  18. curl_setopt($Channel, CURLOPT_FILE, $File);
  19.  
  20. // We are not sending any headers
  21. curl_setopt($Channel, CURLOPT_HEADER, 0);
  22.  
  23. // Disable PEER SSL Verification: If you are not running with SSL or if you don't have valid SSL
  24. curl_setopt($Channel, CURLOPT_SSL_VERIFYPEER, $VerifyPeer);
  25.  
  26. // Disable HOST (the site you are sending request to) SSL Verification,
  27. // if Host can have certificate which is nvalid / expired / not signed by authorized CA.
  28. curl_setopt($Channel, CURLOPT_SSL_VERIFYHOST, $VerifyHost);
  29.  
  30. // Execute CURL command
  31. curl_exec($Channel);
  32.  
  33. // Close the CURL channel
  34. curl_close($Channel);
  35.  
  36. // Close file handle
  37. fclose($File);
  38.  
  39. // return true if file download is successfull
  40. return file_exists($ToLocation);
  41. }
  42. echo file_get_contents("https://www.verisign.com/hp07/i/vlogo.gif");exit;
  43. // Function Usage
  44. if(copySecureFile("https://www.verisign.com/hp07/i/vlogo.gif","c:/verisign_logo.gif"))
  45. {
  46. echo 'File transferred successfully.';
  47. }
  48. else
  49. {
  50. echo 'File transfer failed.';
  51. }
  52. ?>

URL: http://blogs.digitss.com/php/php-downloading-a-file-from-secure-website-https-using-curl/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.