php 2007-2009 Barttos Vartolomei Nicolae All rights reserved http bart

 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
* (c) 2007-2009 Barttos (Vartolomei Nicolae) - All rights reserved.
* http://barttos.net/
* me@barttos.net
* nvartolomei@gmail.com
*/
# error_reporting(E_ALL);
function prea($data) {
echo "<pre>" . $data . "</pre>";
}
function get_temp_fromCache() {
$cache_file = '../cache/weather.ce';
if (file_exists($cache_file)) {
$cache_data = file_get_contents($cache_file);
$cache_data = unserialize($cache_data);
$temp = $cache_data[1];
return $temp;
} else {
return "*°C";
}
}
$cache_file = '../cache/full_weather.ce';
if (file_exists($cache_file)) {
$cache_data = file_get_contents($cache_file);
$cache_data = unserialize($cache_data);
$time = $cache_data[0];
if ($cache_data[0] < time() - 3600) {
$old = TRUE;
# echo "is old!";
} else {
$humidity = $cache_data[1];
$visibility = $cache_data[2];
$condition = $cache_data[3];
$img = $cache_data[4];
# echo 'From cache!';
$old = FALSE;
}
}
if ($old) {
$api_data = file_get_contents("http://weather.yahooapis.com/forecastrss?p=MDXX0002&u=c"); #p = city id #u = celsius or fahrenheit
$mix = '<yweather:atmosphere humidity="';
$pos = strpos($api_data, $mix);
$api_data = substr($api_data, $pos + strlen($mix));
$mix = '"';
$pos = strpos($api_data, $mix);
$humidity = substr($api_data, 0, $pos) . '%'; #umeditatea
$mix = 'visibility="';
$pos = strpos($api_data, $mix);
$api_data = substr($api_data, $pos + strlen($mix));
$mix = '"';
$pos = strpos($api_data, $mix);
$visibility = substr($api_data, 0, $pos) . " km"; #visibilitate
$mix = '<yweather:condition text="';
$pos = strpos($api_data, $mix);
$api_data = substr($api_data, $pos + strlen($mix));
$mix = '"';
$pos = strpos($api_data, $mix);
$condition = substr($api_data, 0, $pos); #conditii
$mix = '<description><![CDATA[
<img src="';
$pos = strpos($api_data, $mix);
$api_data = substr($api_data, $pos + strlen($mix));
$mix = '/>';
$pos = strpos($api_data, $mix);
$img = substr($api_data, 0, $pos);
#write to file
$f = fopen($cache_file, 'w+');
flock($f, LOCK_EX);
$cache_data = array(time(), $humidity, $visibility, $condition, $img);
fwrite($f, serialize($cache_data));
flock($f, LOCK_UN);
fclose($f);
}
echo '<div class="info">Prognoza meteo pentru or. Hincesti</div>';
echo '<br />';
echo '<table cellspacing="0" cellpadding="0">';
echo '<tr>';
echo '<td valign="top"><img src="' . $img . '" alt="' . $condition . ' /></td>';
echo '<td>Temperatura aerului: ' . get_temp_fromCache() . '<br />' . 'Cerul: ' . $condition . '<br /><br />' . 'Vizibilitate: ' . $visibility . "<br />Umeditate: " . $humidity . '</td>';
echo '</tr>';
echo '</table>';
?>