2

I'm trying to get a zip file (I don't know size and name before) with a request xml that return me a zip file. I want to download it but sometimes download it all (16mb about) sometimees not( 2mb or 4 mb or 1mb) I don't know why.

This is my code:

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

I have also tried:

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

Can someone help me?

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171

1 Answers1

8

Try CURLOPT_FILE to download 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);

EDIT:

Notes: As pointed out by @bansi, you might need to verify the file, file size, curl_error, etc.

Community
  • 1
  • 1
Lionel Chan
  • 7,894
  • 5
  • 40
  • 69
  • 1
    I was just writing the same answer, you beat me for a second. Nice. +1 – Dexa Sep 24 '13 at 06:53
  • Also always check if there is any errors before you confirm the downloaded file is correct. http://www.php.net/manual/en/function.curl-error.php – bansi Sep 24 '13 at 06:58
  • Not sure if you need to set `CURLOPT_TIMEOUT` hmm... and yes need to verify the file as well. – Lionel Chan Sep 24 '13 at 06:59
  • It could be the server is not responding you with the correct info? What's in your `XMLRequest`? – Lionel Chan Sep 24 '13 at 07:02
  • no is a right request it's an xml request standard, I have checked with other system if the request is fine and it works, only it doesn't save the file well.. – Alessandro Minoccheri Sep 24 '13 at 07:04
  • Try setting `CURLOPT_RETURNTRANSFER` to 0? – Lionel Chan Sep 24 '13 at 07:05
  • return me always a 0byte file and print me a lot of character coded I don't know which is the problem, the response of the curl is a zip file I know (the server after my xml request return me a zip file contains many xml insides) – Alessandro Minoccheri Sep 24 '13 at 07:18
  • Try removing `CURLOPT_HEADER` instead. I tried on my machine and it should work. See my edited answer – Lionel Chan Sep 24 '13 at 07:36
  • I don't know the problem, with my code in the answer I donwload a file but not total, with your method always 0byte... is very strange the problem... I don't know why I have this problem... – Alessandro Minoccheri Sep 24 '13 at 07:50
  • Oh my, so sorry, returntransfer shouldn't be set!! Remove `CURLOPT_RETURNTRANSFER` – Lionel Chan Sep 24 '13 at 07:55
  • something is moved... 2,9 mb but if I try to extract return me error like that the file isn't complete.. it would be about 15mb.. but now isn't 0 byte, I have to complete the transfer of all file.. step by step :) – Alessandro Minoccheri Sep 24 '13 at 08:13
  • Do not specify CURLOPT_SSLVERSION anymore. Due to POODLE patches on servers, SSL3 will be blocked. Leave CURLOPT_SSLVERSION out completely for the server to auto-negotiate the secure connection. – Exit Dec 26 '14 at 22:01
  • @AlessandroMinoccheri: I know, years ago, but if someone also has the problem that only a part of the data is downloaded: GIVE MORE TIME! `curl_setopt($ch, CURLOPT_TIMEOUT, (60*30)); // 30 minutes` Also of course allow the script to run as long as it is. – Sarah Trees Mar 19 '20 at 12:09