I've prepared a function using cURL to get a remote file to the local storage:
/**
* cURL - Get Remove File
*
* Function using CURL the get a remote file to the local storage.
*
* @param str $url Full remote URL
* @param str $xml_path Internal path to the storage the file
* @param str $xml_file Filename of the xml file to save
* @param arr $access Access credentials (usr;pwd)
*
* @return bollean
*/
function get_remote_file($url, $xml_path, $xml_file, $access = null) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_HEADER, 0);
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,true);
if ($access) {
curl_setopt($curl_handle, CURLOPT_USERPWD, $access["usr"].":".$access["pwd"]);
}
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if ($buffer != false) {
file_put_contents($xml_path.$xml_file, $buffer);
return true;
}
return false;
}
While is working fine, even with password protected files, I'm not too comfortable with CURL, so I wonder if this can be optimized or improve?