2014-03-03 162 views
12

即時通訊使用瀏覽器下載對話框時,試圖從網址下載文件, 但當我使用此代碼時,服務器上的新文件保持空白。 URL的PHP CURL下載文件

$ch = curl_init(); 
$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=$row['certNo']&weight=$row['carat']"; 
curl_setopt($ch, CURLOPT_URL, $source); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$data = curl_exec ($ch); 
curl_close ($ch); 
$destination = "./files/certs/$row['certNo'].pdf"; 
$file = fopen($destination, "w+"); 
fputs($file, $data); 
fclose($file); 

例如:https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35

+0

您需要包裝在'{}'例如數組訪問' 「文本{$陣列[ '鑰匙']}」'。 –

回答

21

我使用此解決了這個問題:

curl_setopt($ch, CURLOPT_SSLVERSION,3); 

這是最後的代碼:

$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $source); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSLVERSION,3); 
$data = curl_exec ($ch); 
$error = curl_error($ch); 
curl_close ($ch); 

$destination = "./files/test.pdf"; 
$file = fopen($destination, "w+"); 
fputs($file, $data); 
fclose($file); 
+1

這並不適用於我,結束了與此解決方案工作:http://stackoverflow.com/questions/31107851 /如何對修復捲曲-35-不能-通信-牢固地與 - 對等無共encryptio –

1
  1. 你檢查,如果結果確實包含數據?
  2. fputs上更大的文件,你需要指定字節長度
  3. 嘗試使用HTTPS連接使用file_put_contents($destination, $data);
+0

1.對$ data進行了回顯並將其清空,我不知道爲什麼。你有什麼想法爲什麼當使用瀏覽器時,我得到一個下載對話框,但與此不起作用? – user3376311

+1

看到您通過SSL請求。如果您不需要爲該URL指定登錄名,請將CURL選項CURLOPT_SSL_VERIFYPEER設置爲false。還要檢查返回的錯誤是什麼,使用$ error = curl_error($ ch);然後打印出來 –

+0

出現此錯誤:未知的SSL協議錯誤連接到myapps.gia.edu:443 – user3376311

2

您的網址。也使用下面的捲曲選項。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
+0

添加了這一行,並得到這個錯誤:未知的SSL協議錯誤連接到myapps.gia.edu:443 – user3376311