All pastes #472710 Raw Edit

barometric.php

public php v1 · immutable
·published 2007-05-05 19:35 UTC
rendered paste body
<?php// -----// Step 0: Configurize where your XML database is// if this file doesn't exist then it will be created$xml_db_loc = "pressures.xml";// -----// Step 1: Attempt to open XML database; if nonexistent, write a blank one in its locationglobal $dom, $pressures;$dom = new DOMDocument;if (file_exists($xml_db_loc)) {	$dom->load($xml_db_loc);	print $dom;	exit;	$pressures = $dom->item(0);	$doc_contents = file_get_contents($xml_db_loc);} else {	$pressures = $dom->appendChild($dom->createElement('pressures'));	$doc_contents = "";}// -----// Step 2: Get your raw weather data// most people will want to use the url// $weather_data = file_get_contents("http://weather.noaa.gov/pub/data/observations/metar/stations/KSLN.TXT");// for testing and debugging purposes we can use a local file, too$weather_data = file_get_contents("./KSLN.TXT");// -----// Step 3: Figure out the time of the given data into happy fun UNIX epoch formdate_default_timezone_set("GMT");$blammo = explode("\n", $weather_data);$datetime = $blammo[0];$timestamp = strtotime($datetime);// -----// Step 3.5: Check if this timestamp has already been doneif(preg_match("/" . $timestamp . "/", $doc_contents)) {	exit();}// -----// Step 4: Figure out what the pressure is$data = $blammo[1];$data_split = explode(" ", $data);$the_data = false;foreach($data_split as $thingy) {	if(preg_match("/^A[0-9][0-9][0-9][0-9]$/", $thingy)) {		$the_data = $thingy;	}	if($the_data) {		break;	}}$pressure_var = substr($the_data, 1, 4) / 100;// -----// Step 5: Throw it in the XML file and write$pressure_tag = $dom->appendChild($pressures->createElement('pressure'));$pressure_tag->setAttribute('timestamp', $timestamp);$pressure_tag->setAttribute('pressure', $pressure_var);$dom->formatOutput = true;$dom->save($xml_db_loc);?>