Execeute code only after download complete and extract PHP - php

I have a file to download via curl in PHP about 16Mb, is a zip file,m I want to download and when is finished to download extract it, after extract parse every file inside it.
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);
$httpHeader2 = array(
"Content-Type: text/xml; charset=UTF-8",
"Content-Encoding: UTF-8"
);
// Execute request, store response and HTTP response code
$xml = curl_exec($ch2);
$this->errno=curl_getinfo( $ch2, CURLINFO_HTTP_CODE );
curl_close($ch2);
$file2 = fopen('json_upload/item.zip','w+');
fwrite($file2, $xml);
fclose($file2);
$zip = new ZipArchive;
echo'<p>json_upload/item.zip</p>';
$zip->open('json_upload/item.zip', ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
$zip->extractTo('json_upload/item/');
$zip->close();
In this mode the file downloaded is 1,6Mb not all and if I try to extract manually return me an error like is damaged.
If I comment the extract code of the zip the file is downloaded completely and if I extract it manually it works fine.
How can I execute the extract of the zip only when the download is complete?
And after how can I execute the parse of each file inside only when the extract command finished?
Thanks

Instead of using fwrite, use file_put_contents()
Replace
$file2 = fopen('json_upload/item.zip','w+');
fwrite($file2, $xml);
fclose($file2);
With
file_put_contents('json_upload/item.zip', $xml);

Related

Curl ignores CURLOPT_FILE

I want to download file with CURLOPT_FILE option.
$file_name = 'm1.dat';
$file = fopen($file_name, 'w');
$ch = curl_init($download_url);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);
fclose($file);
I expect, that file would be saved in $file, but $file is empty, and curl_exec returns content of
the file in $res. So, i must use file_put_contents($file_name, $res) to save the file.
But I want to download BIG files (1Gb and more), and, as I read, downloading whith CURLOPT_FILE is more memory efficient. With file_put_contents my script can run out of memory.
So, why curl not saving file to $file? I use PHP 7.3.9 and I have such problem both on Windows and Linux OS.
From the notes of curl_setopt (https://www.php.net/manual/en/function.curl-setopt.php#99082) it looks as though you should have the CURLOPT_RETURNTRANSFER before the CURLOPT_FILE option...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $file);

Download csv file from server using curl in php

I am struck with this. I want to download a csv file from url using curl. I have referred all the answer in stackoverflow and tried all. But not getting what i am expected. i have the following code.
define("COOKIE_FILE", "cookie.txt");
$path = "settlement_file/test.csv";
set_time_limit(0);
$fp = fopen ($path, 'w+');//This is the file where we save the information
$ch = curl_init(str_replace(" ","%20",$url));//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
You have to remove the curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); statement. It makes curl_exec return the data instead of writing it to a file. Since it comes after the curl_setopt($ch, CURLOPT_FILE, $fp); it overrides that, so just remove the former line.

PHP Curl download File with filename

I have a Curl Download File but The problems I have it's I need to output files with the filename where is output:
example:
$result = false;
$path = "http://mywebsite.com/download.php?file=123";
set_time_limit(0); //prevent timeout
$ch2=curl_init();
$file = fopen('upload/'.$name,'w+');
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_FILE, $file); //auto write to file
curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch2, CURLOPT_POST, 1);
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($file);
In my example, the I need the name of the download file is $name = (the file name)
like if I download file I can return custom_name_file.zip
but I need the writing file will the name of this custom_name_file
any idea?

How to download a zip file and extract and parse xml file in it using PHP cURL

I have a script that usually download xml from an external server with a class.
Now I have with the same code download a zip file, unzip it and this is the xml to parse after.
My initial problem is how to retrieve a zip file unzip and take the xml?
This is my actual code:
$ch2=curl_init();
curl_setopt($ch2, CURLOPT_URL, $this->URL);
curl_setopt($ch2, CURLOPT_TIMEOUT, 540);
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);
$httpHeader2 = array(
"Content-Type: text/xml; charset=UTF-8",
"Content-Encoding: UTF-8"
);
$zip = curl_exec($ch2);
$this->errno=curl_getinfo( $ch2, CURLINFO_HTTP_CODE );
curl_close($ch2);
How to get the zip file and unzip it to retrieve the xml inside?
<?php
//Add this to end of your code
file_put_content('./temp.zip' ,$zip );
$ZipArchive = new ZipArchive;
if ($ZipArchive->open('./temp.zip' ) === TRUE) {
$ZipArchive->extractTo('/my/destination/dir/');
$ZipArchive->close();
echo 'ok';
} else {
echo 'failed';
}
?>

How can I attach a file with a PHP cURL XML Call

I'm using the Amazon AIMS API to upload a an inventory file and I'm having an issue with the cURL call to upload the file. The documentation is very limited, so there is no example code that helps out in this.
This is what I have so far of the cURL call:
// $FILENAME is filename of the CSV file being uploaded:
$inventory = fopen($FILENAME, 'r') or die("Can't open file!");
echo $inventory;
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_INFILE, $inventory);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
curl_setopt($ch, CUROPT_PUT, TRUE);
$response = curl_exec($ch);
curl_close($ch);
I've try putting $inventory into the CURLOPT_POSTFIELDS and not having an INFILE, but I get the same error.
On the XML response, I'm getting "NO_FILE_ATTACHED" so the obvious issue is getting the file to be attached to the XML call.
I also tried uploading as the first responder said using the Example #2 on the curl_setopt page on php.net.
For that, I used the following code:
$data = array('#/tmp/amazon_export.csv');
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
I got the same NO_FILE_ATTACHED response back.
Any ideas?
This works for me:
$hCurl = curl_init();
curl_setopt($hCurl, CURLOPT_PUT, true);
curl_setopt($hCurl, CURLOPT_HEADER, true);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, CURL_TIMEOUT_SECS);
curl_setopt($hCurl, CURLOPT_URL, "$oMessage->url/att/$fid");
curl_setopt($hCurl, CURLOPT_HTTPHEADER, $aCurlHeaders);
// TODO it could be possible that fopen() would return an invalid handle or not work altogether. Should handle that
$fp = fopen ($finfo['tmp_name'], "r");
curl_setopt($hCurl, CURLOPT_INFILE, $fp);
curl_setopt($hCurl, CURLOPT_INFILESIZE, $finfo['size']);
$sResp = curl_exec($hCurl);
You are combining PUT and POST in a single curl operation, which will not work. Refer to example #2 of the curl_setopt manual page for an example on how to upload a file using POST.
you have $filename and $FILENAME, the filesize call should be in your case filesize($FILENAME)...
Hope thats helps

Categories