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

		// Close the CURL channel

		// Close file handle

		// return true if file download is successfull
		return file_exists($ToLocation);
	 *	This code don't work
	 *	echo file_get_contents("");		
	// Function Usage
		echo 'File transferred successfully.';
		echo 'File transfer failed.';

Please let me know if there are any other ways of doing that; other than this work around.