簡體   English   中英

PHP CURL 下載文件

[英]PHP CURL Download File

我試圖從 url 下載文件,當我使用瀏覽器時,下載對話框有效,但是當我使用此代碼時,我服務器上的新文件保持為空。

$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

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

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);

您的網址使用 https 連接。 也使用以下 curl 選項。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  1. 您是否檢查過結果是否確實包含數據?
  2. 在較大的文件上使用fputs ,您需要指定字節長度
  3. 嘗試使用file_put_contents($destination, $data);

看看是否可以使用以下代碼來幫助您。

//創建一個cURL句柄。 $ch = curl_init($fileUrl);

//將我們的文件句柄傳遞給cURL。 curl_setopt($ch, CURLOPT_FILE, $fp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM