Recently in one of my project I was suppose to download file from a secure website. Actually I have to set the scheduler job for downloading nightly builds/full files from the server and the files are placed on secure website.
Initially I tried that with file_get_contents function but it was just downloading 0KB file. Then it strike me that oh..! I can't use file_get_contents as it can't understand the HTTPS protocol here. So what could help me nothing else then our best friend CURL. file_get_contents and fopen kind of other functions works fine with any http web locations.
Here is the simple CURL file transfer function snippet which will copy file from any secure http location.
/** * Copy File from HTTPS/SSL location * * @param string $FromLocation * @param string $ToLocation * @return boolean */ function copySecureFile($FromLocation,$ToLocation,$VerifyPeer=false,$VerifyHost=true) { // Initialize CURL with providing full https URL of the file location $Channel = curl_init($FromLocation); // Open file handle at the location you want to copy the file: destination path at local drive $File = fopen ($ToLocation, "w"); // Set CURL options curl_setopt($Channel, CURLOPT_FILE, $File); // We are not sending any headers curl_setopt($Channel, CURLOPT_HEADER, 0); // Disable PEER SSL Verification: If you are not running with SSL or if you don't have valid SSL curl_setopt($Channel, CURLOPT_SSL_VERIFYPEER, $VerifyPeer); // Disable HOST (the site you are sending request to) SSL Verification, // if Host can have certificate which is nvalid / expired / not signed by authorized CA. curl_setopt($Channel, CURLOPT_SSL_VERIFYHOST, $VerifyHost); // Execute CURL command curl_exec($Channel); // Close the CURL channel curl_close($Channel); // Close file handle fclose($File); // return true if file download is successfull return file_exists($ToLocation); } /** * This code don't work * echo file_get_contents("https://www.verisign.com/hp07/i/vlogo.gif"); **/ // Function Usage if(copySecureFile("https://www.verisign.com/hp07/i/vlogo.gif","c:/verisign_logo.gif")) { echo 'File transferred successfully.'; } else { echo 'File transfer failed.'; }
Please let me know if there are any other ways of doing that; other than this work around.
#1 by Loïc Hoguin on October 26, 2008 - 9:06 am
Quote
According to the documentation, you can use file_get_contents and friends with HTTPS (see php.net/wrappers). A simple 'echo file_get_contents("https://www.verisign.com/hp07/i/vlogo.gif"
;' works perfectly fine here.
What would be interesting to know is what exactly went wrong for you when you tried to use this function.
#2 by Dharmavirsinh Jhala on October 26, 2008 - 4:07 pm
Quote
Hi Loic,
Actually it doesn't work that way..
If I write following line of code:
echo file_get_contents("https://www.verisign.com/hp07/i/vlogo.gif"
;
I am getting following error:
Warning: file_get_contents (https://www.verisign.com/hp07/i/vlogo.gif) [function.file-get-contents]: failed to open stream: Invalid argument in ..htdocscopySecureFile.php on line 3
If you are find it working then let me know if there is any setting to work out this error.
#3 by Loïc Hoguin on October 27, 2008 - 9:03 pm
Quote
Hello,
Was your PHP built with OpenSSL enabled?
If you have a command line try
echo " enabled
Otherwise it won't work with HTTPS. If you recompile PHP with OpenSSL support
file_get_contents('https') should work.
Greetings,
#4 by k on November 20, 2008 - 1:47 pm
Quote
i got an error.
warning: file_get_contents(): ssl: connection reset by peer in /var/www/vhosts/test.php
warning: file_get_contents(): ssl: fatal protocol error in /var/www/vhosts/test.php
#5 by Stu on March 2, 2009 - 12:03 pm
Quote
Nice Script. Seems to work right away.
Thanks!
#6 by donagorr on December 27, 2009 - 4:29 am
Quote
I congratulate, your idea is useful
#7 by Anil on May 2, 2011 - 12:17 am
Quote
As documented on the PHP site at http://php.net/manual/en/function.file-get-conten… calls using HTTPS where the site is running an MS IIS server, using file_get_contents will produce an "SSL: fatal protocol error".
Could you verify if the call you were making was to a MS IIS Server?
#8 by Dharmavirsinh Jhala on May 2, 2011 - 11:57 pm
Quote
Hello Anil,
There can be 2 reasons:
1) PHP openssl extension is disabled:
It doesn\’t matter whether the server being called is running on IIS or Apache. This error arises because of PHP is not compiled with OpenSSL and/or php_openssl extension is not enabled. In this case just uncomment and it will work.
2) another case is just the way you mentioned, due to some IIS bug, which you can use CURL to fix or lower down and ignore the warning if it works.
Yes, I had IIS serving that file, which I was trying to download.
Pingback: file_get_contents fails with HTTPS URL - Anil Natha [dot] com