【问题标题】:Download zip PHP下载压缩包 PHP
【发布时间】:2016-07-14 10:09:48
【问题描述】:

我正在尝试获取一个带有请求 xml 的 zip 文件(我以前不知道大小和名称),该请求 xml 向我返回了一个 zip 文件。我想下载它,但有时全部下载(大约 16mb)有时不下载(2mb 或 4m​​b 或 1mb)我不知道为什么。

这是我的代码:

$ch2=curl_init();
curl_setopt($ch2, CURLOPT_URL, $this->URL);
curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS,$this->XMLRequest);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch2, CURLOPT_SSLVERSION, 3); 
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);

$xml = curl_exec($ch2);

curl_close($ch2);
$file2 = fopen('upload/item.zip','w+');
fwrite($file2, $xml);
fclose($file2);

我也试过了:

file_put_contents('upload/item.zip', $xml);

有人可以帮我吗?

【问题讨论】:

  • 我没有在你的代码中看到 curl_exec?是 $xml = curl_exec( $ch2 );
  • 编辑问题我缺少一行@Dexa

标签: php curl


【解决方案1】:

试试CURLOPT_FILEdownload large file

set_time_limit(0); //prevent timeout

$ch2=curl_init();
$file2 = fopen('upload/item.zip','w+');
curl_setopt($ch2, CURLOPT_URL, $this->URL);

curl_setopt($ch2, CURLOPT_FILE, $file2); //auto write to file

curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS,$this->XMLRequest);
# don't use this. please verify your host & peer properly :)
# curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
# curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch2, CURLOPT_SSLVERSION, 3); 
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);

curl_exec($ch2);

curl_close($ch2);
fclose($file2);

编辑:

注意:正如@bansi 所指出的,您可能需要验证文件、文件大小、curl_error 等。

【讨论】:

  • 我只是在写同样的答案,你打败了我一秒钟。好的。 +1
  • 在确认下载的文件正确之前,还要经常检查是否有任何错误。 php.net/manual/en/function.curl-error.php
  • 不确定是否需要设置CURLOPT_TIMEOUT hmm...是的,还需要验证文件。
  • 可能是服务器没有以正确的信息响应您?你的XMLRequest 里有什么?
  • no 是正确的请求,它是 xml 请求标准,我已经与其他系统检查过请求是否正常并且可以正常工作,只是它不能很好地保存文件..
猜你喜欢
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
相关资源
最近更新 更多