PHP CURL 下载文件

2022-08-30 23:10:22

im尝试从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


答案 1

我用这个解决了这个问题:

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

答案 2

您的网址使用 https 连接。也使用以下卷曲选项。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

推荐