Michel Plungjan
asked on
What am I missing in this curl and unzip?
Here is a very simple program to download and unzip a file
It creates a file and a dir but the file is empty and so is the dir
If I echo $file_to_open I actually see the file contents but I want to write the zip - actually I do not need to write the zip, I just want the unzipped contents in a dir named after the file so if I can unzip without saving the file, I would be quite happy too.
<?PHP
$appNumber = "12102391";
$payload = file_get_contents('http://storage.googleapis.com/uspto-pair/applications/'.$appNumber.'.zip'); //grab the file from the remote server
$target = "."; // this is the destination for the unzipped files
openZip($payload,$appNumber);
function openZip($file_to_open,$appNumber) {
global $target;
$file = $appNumber.'.zip';
$client = curl_init($file_to_open);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$fileData = curl_exec($client);
echo("---\n".$file."-".$fileData."\n---\n");
file_put_contents($file, $fileData);
@unlink($file);
touch($file);
$zip = new ZipArchive();
$f = $zip->open($file);
var_dump($f);
$zip->extractTo($appNumber);
$zip->close();
var_dump(glob($appNumber.'/*'));
}
?>
It creates a file and a dir but the file is empty and so is the dir
If I echo $file_to_open I actually see the file contents but I want to write the zip - actually I do not need to write the zip, I just want the unzipped contents in a dir named after the file so if I can unzip without saving the file, I would be quite happy too.
The zip file has to be saved, you cannot use ZipArchive with an in memory object.
Here's a clue to why you have a blank file...
@unlink($file);
Here's a clue to why you have a blank file...
@unlink($file);
ASKER
Thanks for the reply
I need to download any zip on demand from that source. Later I will produce an HTML file with links to the pdfs in the zip
I can easily download and unzip using windows
The problem does not seem to be the unzipping (yet) but the download and saving.
I am doing something wrong in the Curl for now
I need to download any zip on demand from that source. Later I will produce an HTML file with links to the pdfs in the zip
I can easily download and unzip using windows
The problem does not seem to be the unzipping (yet) but the download and saving.
I am doing something wrong in the Curl for now
ASKER
I was suspicious of the unlink but I have now commented it out. No change to the results
Michel, please describe the app in layman's terms. You read from USPTO, but what are you trying to do with cURL?
Have a look at the output from just this script. It looks like some of the data is ZIP and some is clear text.
<?php // RAY_temp_mplungjan.php
error_reporting(E_ALL);
$appNumber = "12102391";
$payload = file_get_contents('http://storage.googleapis.com/uspto-pair/applications/'.$appNumber.'.zip'); //grab the file from the remote server
var_dump($payload);
die();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect - thanks
ASKER
Ray: I thought I needed CURL to get the file. I was mixing my tiny knowledge of get_contents, thinking I needed curl for binary contents
The program is supposed to - given an application number, access the zipfiles from google, unzipping and presenting a clickable list of links to them
The program is supposed to - given an application number, access the zipfiles from google, unzipping and presenting a clickable list of links to them
Where cURL is more useful than file_get_contents() is when you do not want to have a dependency on the remote resource. If the remote resource hangs, your script hangs until PHP times out and produces a fatal error - maybe 30 seconds in the future. With cURL you can set a much shorter timeout and you can add error handling to degrade gracefully.
ASKER
ah! thanks
If you can get the file downloaded to a Windows7 desktop, you can copy the contents of the zip file to another directory and Windows will unzip it for you in the process.
The comments on this page and the linked pages tell something of a tale of woe.
http://php.net/manual/en/class.ziparchive.php