WeatherAjax.php
<?php
header ('Content-Type: application/xml');
echo '<?xml version="1.0"?>'."\n";
// start root element:
echo "<weather>\n";
// get current weather conditions:
$theURL="http://weather.noaa.gov/weather/current/KSKX.html";
if (!($contents = file_get_contents ($theURL)))
{
echo '<error>Error: Could not open weather site ('.$theURL.").</error>\n</weather>\n";
exit;
}
$contents = strip_tags ($contents);
// write temperature element:
echo '<temp>';
$pattern = 'Temperature[[:space:]]+([[:print:]]+)';
if (ereg ($pattern, $contents, $result))
{
echo $result[1]."</temp>\n";
}
else
{
echo "can't find temperature</temp>\n";
};
// write sky conditions:
echo '<sky>';
$pattern = 'Sky conditions[[:space:]]+([[:print:]]+)';
if (ereg ($pattern, $contents, $result))
{
echo $result[1]."</sky>\n";
}
else
{
echo "can't find sky conditions</sky>\n";
};
// write wind:
echo '<wind>';
$pattern = 'Wind[[:space:]]+([[:print:]]+)';
if (ereg ($pattern, $contents, $result))
{
echo $result[1]."</wind>\n";
}
else
{
echo "can't find wind conditions</wind>\n";
};
/* debugging element: if the value of this element changes on each XMLHttpRequest request, then the browser is
actually obtaining new data rather than simply displaying old cached data: */
echo '<rand>'.rand()."</rand>\n";
// close root element:
echo "</weather>\n";
?>