PHP Curl downloads empty file(.gz) from a https URL

Wednesday, November 4, 2015

I'm trying to download a .xml.gz file from a https URL with authentication.



Here is my current code.



$remote_file = 'https://path/filename.xml.gz';
$local_file = "test.xml.gz";
$username ="21";
$password ="qwerty";

$ch = curl_init($remote_file);
$headers = array('Content-type: application/x-gzip','Connection: Close');
$fp = fopen ($local_file, 'wb');

curl_setopt($ch, CURLOPT_URL,$remote_file);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_exec($ch);
curl_close($ch);
fclose($fp);


After execution, the test.xml.gz file is created but empty.

0 comments:

Post a Comment