Link to home
Start Free TrialLog in
Avatar of IT_Architect
IT_Architect

asked on

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

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!
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As you wish... What are you going to do with all the little XML files?  There are a few thousand, I think.
Avatar of IT_Architect
IT_Architect

ASKER

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.
Because it was agreed that it was good like it is.