[英]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);
fputs
,您需要指定字節長度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.