我試圖從https URL下載帶驗證的.xml.gz文件。PHP Curl從https URL下載空文件(.gz)
這是我目前的代碼。
$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);
$data = curl_exec($ch);
if(fwrite($fp,$data))
{
echo "success";
}
else
{
echo "fail";
}
curl_close($ch);
fclose($fp);
執行後,test.xml.gz文件被創建,但是爲空。
我認爲問題在於連接到https頁面中的文件。當我嘗試從非https網址下載文件時,代碼似乎工作正常。
奇怪的是,捲曲顯示沒有錯誤任何
@ Heru-Luin好點,更正! – Ashley
嗨,我也試過。似乎沒有工作。我已更新問題詳細信息中的代碼 –
如果輸出$ data的內容,您會得到什麼?另外,看看你是否使用curl_error($ ch)得到捲曲錯誤將是一個不錯的選擇。 – Ashley