Jump to content

unzip file from URL without downloading it and reading contents


OsvaldoM

Recommended Posts

Hello everyone,

i've been stuck for 2 days with a little piece of code that keeps giving me problems. Here is the deal:

 

I'm connecting to the MSN Adcenter API, which for some reports it provides an xml withina zip file for download. Everything used to be fine running under localhost until i uploaded it to my server and i found out i do not have write permissions in the folder.

So basically the question is, how to get the zip, unzip it and then get the contents of the xml without actually downloading any of the files?.

 

I was able to read the zip contents without actually having to unzip the xml, though as for how to do it with the zip file im stuck. Here is the code i have so far.

 

$ch = curl_init($downloadURL);

		 if (! $ch) {
		die( "Cannot allocate a new PHP-CURL handle" );
		}


		//$fp = fopen (dirname(__FILE__) . '/' . $fileName, 'w+');//This is the file where we save the information

		curl_setopt($ch, CURLOPT_HEADER, false);  // Don’t return the header, just the html
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return contents as a string
		curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);

		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);

		//curl_setopt($ch, CURLOPT_FILE, $fp);
		curl_setopt($ch, CURLOPT_TIMEOUT, 50);

		// CHECK THIS ONE 
		curl_setopt($ch, CURLOPT_URL, $downloadURL);
		//curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

		// Avoids error
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Shouldn't be used
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/GTECyberTrustGlobalRoot.crt");

		curl_setopt($ch, CURLOPT_PROXY, "XXXXXX.proxy.XXXXXX.XXXXX.com");
		curl_setopt($ch, CURLOPT_PROXYPORT, XXXXXX);
		//	curl_setopt ($ch, CURLOPT_PROXYUSERPWD, "username:password");

		$data = curl_exec ($ch);
		//print $data;

		echo gettype($data);

		//var_dump($data);
		// var_dump($fp);


		if (!$data) {
		echo "<br />cURL error number:" .curl_errno($ch);
		echo "<br />cURL error:" . curl_error($ch);
		exit;
		} else {
		echo "<br>curl succeeded with" . $fileName;
		}

		// echo var_dump($data);
		curl_close($ch);


		//=============================================





		//=================================

		$zip = zip_open($fileName);
		//$zip = getcwd() . '/$fileName';
			if (is_resource($zip)) {
			echo "reading the zip";
				while ($zip_entry = zip_read($zip)) {
					echo "Name:               " . zip_entry_name($zip_entry) . "\n";
					echo "Actual Filesize:    " . zip_entry_filesize($zip_entry) . "\n";
					echo "Compressed Size:    " . zip_entry_compressedsize($zip_entry) . "\n";
					echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";

					if (zip_entry_open($zip, $zip_entry, "r")) {
						echo "File Contents:\n";
						$contentsXML = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
						$data = simplexml_load_string($contentsXML);
						zip_entry_close($zip_entry);
					}
					echo "\n";

				}

				zip_close($zip);

			} else {
			echo "It is not a resource";
			}

 

 

So basically, i believe what i am looking for is to "unzip" the $data returned from the cURL? Any suggestions?

Thanks

 

Im running:

PHP Version 5.3.1

zlib, compress.bzip2, phar, zip Enabled

Link to comment
Share on other sites

Ok, so after reading and reading, finding unanswered posts all over the web about the same topic and trying different approaches I believe there is no way to hold the results, then unzip, then read the xml without actually downloading the file. I'd tried cookies, system stores and the results where the same, the most i got was to store the entire results in system cache, but couldn't advance from there. Hope this conclusion of mine helps and saves time to other people looking to do the same approach.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.