Solved

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

Posted on 2016-11-16
5
16 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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 …

733 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