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);
file_put_contents('upload/item.zip', $xml);
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.