PHP CURL 下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22155882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:42:12  来源:igfitidea点击:

PHP CURL Download File

phpfilecurldownload

提问by user3376311

im trying to download a file from a url, when I use the browser the download dialog works but when I use this code the new file on my server stay empty.

我试图从 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);

example of url: https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35

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

回答by user3376311

I solved this problem using this:

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

curl_setopt($ch, CURLOPT_SSLVERSION,3);

This is the final code:

这是最终的代码:

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

回答by Sabuj Hassan

Your url using https connection. Use the following curl option as well.

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

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

回答by Rok Nemet

  1. Did you check if result actually contains data?
  2. with fputson larger files you need to specify byte length
  3. Try using file_put_contents($destination, $data);
  1. 您是否检查过结果是否确实包含数据?
  2. fputs上更大的文件,你需要指定字节长度
  3. 尝试使用 file_put_contents($destination, $data);