Solved

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

Posted on 2016-11-16
5
10 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
  • 3
  • 2
5 Comments
 
LVL 108

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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now