With PHP 4, we have to use the old fashioned way of [man]file_get_contents/man and RegEx. Unless you have the PECL DOMXML installed on PHP4, this is the easy way to do it:
-
Get the feed into a string:
<?php
$weather = file_get_contents("http://www.weather.gov/data/current_obs/KBWI.xml");
-
Set up some RegEx patterns:
$location = "@<location>([a-zA-Z0-9\,\-\_\s]*)</location>@";
$farenheit = "@<temp_f>([0-9]*)</temp_f>@";
$celcius = "@<temp_c>([0-9]*)</temp_c>@";
$condition = "@<weather>([a-zA-Z\s]*)</weather>@";
$obsvd = "@<observation_time_rfc822>([a-zA-Z0-9\,\:\-\s]*)</observation_time_rfc822>@";
-
Run them through preg_match_all()
preg_match($location, $weather, $matches);
$info['loc'] = $matches[1];
preg_match($farenheit, $weather, $matches);
$info['deg_f'] = $matches[1];
preg_match($celcius, $weather, $matches);
$info['deg_c'] = $matches[1];
preg_match($condition, $weather, $matches);
$info['condition'] = $matches[1];
preg_match($obsvd, $weather, $matches);
$info['observed'] = $matches[1];
-
Display our results:
echo 'Temp: '.$info['deg_f'].' °F ('.$info['deg_c'].' °C)<br>
Observed from '.$info['loc'].' at '.$info['observed'];
?>
So the entire thing would be:
<?php
## GET WEATHER INFO TO STRING
$weather = file_get_contents("http://www.weather.gov/data/current_obs/KBWI.xml");
## SET UP PATTERNS
$location = "@<location>([a-zA-Z0-9\,\-\_\s]*)</location>@";
$farenheit = "@<temp_f>([0-9]*)</temp_f>@";
$celcius = "@<temp_c>([0-9]*)</temp_c>@";
$condition = "@<weather>([a-zA-Z\s]*)</weather>@";
$obsvd = "@<observation_time_rfc822>([a-zA-Z0-9\,\:\-\s]*)</observation_time_rfc822>@";
## MATCH PATTERNS, GET INFORMATION
preg_match($location, $weather, $matches);
$info['loc'] = $matches[1];
preg_match($farenheit, $weather, $matches);
$info['deg_f'] = $matches[1];
preg_match($celcius, $weather, $matches);
$info['deg_c'] = $matches[1];
preg_match($condition, $weather, $matches);
$info['condition'] = $matches[1];
preg_match($obsvd, $weather, $matches);
$info['observed'] = $matches[1];
## DISPLAY INFORMATION
echo 'Temp: '.$info['deg_f'].' °F ('.$info['deg_c'].' °C)<br>
Observed from '.$info['loc'].' at '.$info['observed'];
?>
Giving us the following output:
Temp: 51 °F (11 °C)
Observed from Baltimore-Washington International Airport, MD at Mon, 30 Jan 2006 21:54:00 -0500 EST