/
weather.php
49 lines (39 loc) · 1.49 KB
/
weather.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
class Weather {
public function show($zipcode) {
$result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=f');
$xml = simplexml_load_string($result);
$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$location = $xml->channel->xpath('yweather:location');
if(!empty($location)){
foreach($xml->channel->item as $item){
$current = $item->xpath('yweather:condition');
$forecast = $item->xpath('yweather:forecast');
$current = $current[0];
?>
<h1 style="margin-bottom: 0">Weather for <?php echo $location[0]['city']; ?>, <?php echo $location[0]['region']; ?></h1>
<small><?php echo$current['date']; ?></small>
<h2>Current Conditions</h2>
<p>
<span style="font-size:72px; font-weight:bold;"><?php $current['temp']; ?>°C</span>
<br/>
<img src="http://l.yimg.com/a/i/us/we/52/{$current['code']}.gif" style="vertical-align: middle;"/>
<?php echo $current['text']; ?>
</p>
<h2>Forecast</h2>
<?php echo $forecast[0]['day'];?> - <?php echo $forecast[0]['text']; ?>. High: <?php echo $forecast[0]['high']; ?> Low: <?php echo $forecast[0]['low']; ?>
<br/>
<?php echo $forecast[1]['day']; ?> - <?php echo $forecast[1]['text']; ?>. High: ?php echo $forecast[1]['high']; ?> Low: <?php $forecast[1]['low']; ?>
</p>
<?php
}
}else{
echo '<h1>No results found.</h1>';
}
}
}
function weather($city) {
$t=new Weather();
$t->show($city);
}
?>