Solved

I need your suggestions on the following code to read xml files inside of a zip file.

Posted on 2016-11-16
5
19 Views
Last Modified: 2016-11-21
This is the code.  It seems to work fine, but perhaps it can be improved.
#!/usr/local/bin/php
<?php
error_reporting(E_ALL);

// Download Currents Zip file
$url = "http://w1.weather.gov/xml/current_obs/all_xml.zip";
$filename = "all_xml.zip";
$retval = shell_exec("fetch $url 2>&1");		// Add error handling to make sure you get a new file

// Open Zip file
$file = getcwd() . '/' . $filename;				// Make up full path to file
$zip = zip_open($file);							// Return handle to file

// Loop here to read zip file and store new currents
  $zip_entry = zip_read($zip); 					// Return next directory entry or FALSE add code to break loop
  $xml_name = zip_entry_name($zip_entry);		// Return name of directory entry
  // Figure out how skip $xml_names that begin with a number.
  $retval = zip_entry_open($zip,$zip_entry);	// Open directory entry. Returns True on success.  Add error handling
  $xml = zip_entry_read ($zip_entry,1048576);	// Assign file content to string.  empty string EOF, or FALSE on error.  Add error handling
  $retval = zip_entry_close($zip_entry);		// Returns True on success.  Probably skip error handling.
  $obj = SimpleXML_Load_String($xml);			// Returns False on failure.  Add error handling
  // Code here to store new current in database
// End of loop

// Close Zip file
$retval = zip_close($zip);						// Returns True on success.  Probably skip error handling.

// **** Test area
// echo PHP_EOL . "Before XML dump";
// var_dump($xml);
// echo PHP_EOL . "After XML dump";
// echo PHP_EOL . "Before Obj dump";
var_dump($obj);
// echo PHP_EOL . "After Obj dump";

Open in new window

Thank you!
0
Comment
Question by:IT_Architect
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 41889717
You could probably read the Zipped XML with file_get_contents() or cURL.  But if it works fine, why mess with it?

There are a ton of little files inside the zip archive.
0
 

Accepted Solution

by:
IT_Architect earned 0 total points
ID: 41889745
I think I'll stick with it.
- shell_exec gives me good error checking
- fetch automatically trashes the previous existing all_xml.zip
- zip_entry_read means I don't have to explode it into lots of files to be read from a directory
- It's definitely fast
- It works perfectly

Thanks tons...again :D
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41889772
As you wish... What are you going to do with all the little XML files?  There are a few thousand, I think.
0
 

Author Comment

by:IT_Architect
ID: 41889816
What are you going to do with all the little XML files?  There are a few thousand, I think.
They are ICAO current weather observations that we store in a MySQL database.  They are replaced every 15 minutes and individual requests don't make sense.  Since they are ICAOs, they are world-wide.

I would do bulk for the forecasts you helped me with earlier, but I cannot find a good source at Weather.gov for bulk forecasts, and the source I had where I could buy the information already consolidated stopped selling it and others are crazy expensive for free information.  Forecasts are updated once per hour, and I can pick up all zones in about 15 minutes doing only a few a second.  Forecasts cover only the US and it is one forecast per county.
0
 

Author Closing Comment

by:IT_Architect
ID: 41895656
Because it was agreed that it was good like it is.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question