Hi, im trying to get my parsed results to dump into a txt file on my server. . . but i cant seem to get past some glitches.
A second pair of eyes will be appreciated. . .
Below is one of my weather scripts -- where it now is 'echo'ing the results -- i want to have it ultimately write the results to a txt file.
Ive been trying to get it work with one FWrite in the first function before i convert the remaining -- and ive managed to only get the following 2 results after a lot of tinkering
1) A 0 bytes file (even though the data in the first function is sucessfully echoed in the next statement to show that it's being parsed successfully
2) A file with the data string -- only to be followed by extra spaces,a line break, and more spaces in the txt file. . (though not reflected in the echo results)
Any help will be appreciated!
---------------------------------------
<?php
require("MagicParser.php");
function metarrecord($metarrecord)
{
fwrite($currentfile, $metarrecord);
echo $metarrecord["WEATHER"];
}
function currentweatherHandler($currentrecord)
{
// This is where you write your code to process each record, such as loading a database
// Here we just display the record contents using PHP's internal print_r() function
echo " & ".round($currentrecord["AWS:OB/AWS:TEMP"])."°<br>";
if (round($currentrecord["AWS:OB/AWS:TEMP"]) <> $currentrecord["AWS:OB/AWS:FEELS-LIKE"]) echo "(Feels Like: ".$currentrecord["AWS:OB/AWS:FEELS-LIKE"]."°)";
echo "</strong></u>Winds ".$currentrecord["AWS:OB/AWS:WIND-DIRECTION"]." @ ".$currentrecord["AWS:OB/AWS:WIND-SPEED"];
if ($currentrecord["AWS:OB/AWS:GUST-SPEED"] <> 0) echo "<BR>(Gusting ".$currentrecord["AWS:OB/AWS:GUST-DIRECTION"]." @ ".$currentrecord["AWS:OB/AWS:GUST-SPEED"].")";
echo "<br><br>Humidity ".$currentrecord["AWS:OB/AWS:HUMIDITY"]."%"."<br>";
echo "Pressure ".$currentrecord["AWS:OB/AWS:PRESSURE"]."in"."<br>";
echo "Dewpoint ".round($currentrecord["AWS:OB/AWS:WET-BULB"])."°<br>";
echo "(As Of ".$currentrecord["AWS:OB/AWS:OB-DATE/AWS:HOUR-NUMBER"].":".$currentrecord["AWS:OB/AWS:OB-DATE/AWS:MINUTE-NUMBER"].":".$currentrecord["AWS:OB/AWS:OB-DATE/AWS:SECOND-NUMBER"]." ".$currentrecord["AWS:OB/AWS:OB-DATE/AWS:AM-PM-ABBRV"]." ".$currentrecord["AWS:OB/AWS:OB-DATE/AWS:TIME-ZONE-ABBRV"].")";
}
$currentfile = fopen("current.txt", "w");
MagicParser_parse("http://www.nws.noaa.gov/data/current_obs/KDTO.xml","metarrecord","xml|CURRENT_OBSERVATION/");
MagicParser_parse("http://A5574682774.api.wxbug.net/getLiveWeather.aspx?acode=A5574682774&zipcode=76207&StationID=DNTNP","currentweatherHandler","xml|AWS:WEATHER/");
fclose($currentfile);
?>
Hey dave -- thanx for your quick help.
That definately got me closer -- but not a total success
Ive taken what you gave, and did some other tweaking to rearange the outputted data. . . here's where im at on the script
----------------
<?php
require("/homepages/16/d161544468/htdocs/weather/MagicParser.php");
function metarrecord($metarrecord)
{
global $weather1;
$weather1 = $metarrecord["WEATHER"];
}
function currentweatherHandler($currentrecord)
{
// This is where you write your code to process each record, such as loading a database
// Here we just display the record contents using PHP's internal print_r() function
global $weather10;
global $weather2;
$weather2 = round($currentrecord["AWS:OB/AWS:TEMP"]);
if (round($currentrecord["AWS:OB/AWS:TEMP"]) <> $currentrecord["AWS:OB/AWS:FEELS-LIKE"]) $weather3 = "(Feels Like: ".$currentrecord["AWS:OB/AWS:FEELS-LIKE"]."°)<br>";
$weather4 = "</strong></u>Winds ".$currentrecord["AWS:OB/AWS:WIND-DIRECTION"]." @ ".$currentrecord["AWS:OB/AWS:WIND-SPEED"];
$weather6 = "<br><br>Humidity ".$currentrecord["AWS:OB/AWS:HUMIDITY"]."%"."<br>";
$weather7 = "Pressure ".$currentrecord["AWS:OB/AWS:PRESSURE"]."in"."<br>";
$weather8 = "Dewpoint ".round($currentrecord["AWS:OB/AWS:WET-BULB"])."°";
$weather10 = $weather3.$weather4.$weather6.$weather7.$weather8;
}
global $weatherall;
MagicParser_parse("http://www.nws.noaa.gov/data/current_obs/KDTO.xml","metarrecord","xml|CURRENT_OBSERVATION/");
MagicParser_parse("http://A5574682774.api.wxbug.net/getLiveWeather.aspx?acode=A5574682774&zipcode=76207&StationID=DNTNP","currentweatherHandler","xml|AWS:WEATHER/");
$weatherall = "document.write('<u><strong>".$weather2."° & ".$weather1."</u></strong><br>".$weather10."');";
$currentfile = fopen("/homepages/16/d161544468/htdocs/weather/current.js", "wt");
fwrite($currentfile, $weatherall);
fclose($currentfile);
echo $weatherall ;
?>
Here's what i get on my end --
Via the echo statements -- it's all good -- all one line, and perfect for a javascript txt file
(Output results via echo statements)
document.write('<u><strong>64° & Overcast</u></strong><br></strong></u>Winds S @ 7<br><br>Humidity 68%<br>Pressure 30.03in<br>Dewpoint 58°');
(Output written to the actual file)
document.write('<u><strong>64° & Overcast
</u></strong><br></strong></u>Winds S @ 7<br><br>Humidity 68%<br>Pressure 30.03in<br>Dewpoint 58°');
Note the line break (+7 spaces) between $metarrecord["WEATHER"]; and the next line. Ive tried moving the varible around, and it's the same result.
It now almost looks like a parsing glitch -- but it's not reflected in the echo results, so i dont know how that could be
OR somehow a function glitch, but why it's only affecting this varible, i dont see either. . .
Ive tried moving the file write command into the function itself -- but had the same results
If it's helpful, it's in action at:
PHP Script -- www.christoperj.com/weather/current.php
JS Result -- www.christoperj.com/weather/current.js
Any help, always appreciated
Hi Christopher,
What's happening is that the data in $weather1 contains a new line. The reason you don't see it in the echo'd version is because new lines are not displayed within HTML - the character is ignore. However, in your text file; the new line is apparent because it is meaningfull in that context.
PHP's trim() function will remove new line characters from the end of a string, so the thing to do is use this within your script anywhere you want to make sure that data from the feed is clean, for example, to remove white space (including new lines) from $weather1, use:
$weatherall = "document.write('<u><strong>".$weather2."° & ".trim($weather1)."</u></strong><br>".$weather10."');";
Hope this helps,
Cheers,
David.
That did it -- you really are the man :P
thanx again for your help
Hi Christopher,
The only problem I can see there is that you have not declared $currentfile as global in your metarrecord() function; and therefore the fwrite call would have failed.
<?php
function metarrecord($metarrecord)
{
// need to declare $currentfile as global here
global $currentfile;
fwrite($currentfile, $metarrecord);
echo $metarrecord["WEATHER"];
}
?>
Hope this helps!
Cheers,
David.