This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgweather.class.php
99 lines (75 loc) · 2.96 KB
/
gweather.class.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
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
94
95
96
97
98
99
<?php
class gWeather {
private $options = array(); // array values: url_get, language, encoding
public $information;
public $current;
public $forecast_list;
public function __construct($options = false) {
if (empty($options)) {
$this->options = array(
'url_get' => 'http://www.google.com/ig/api?weather=',
'language' => 'pt-br',
'encoding' => 'utf8'
);
}
}
public function getData($params) {
if (!empty($params['cidade']) AND !empty($params['pais'])) {
$url = $this->options['url_get'] . urlencode(trim($params['cidade'])) . ',' . trim($params['pais']) . '&hl=' . $this->options['language'] . '&oe=' . $this->options['encoding'];
} else if (!empty($params['latitude']) AND !empty($params['longitude'])) {
$url = $this->options['url_get'] . ',,,' . urlencode(trim($params['latitude'])) . ',' . trim($params['longitude']) . '&hl=' . $this->options['language'] . '&oe=' . $this->options['encoding'];
}
$xml = simplexml_load_file($url);
if ($xml) {
$this->setForecast($xml->xpath('/xml_api_reply/weather/forecast_conditions'));
$this->setInformation($xml->xpath('/xml_api_reply/weather/forecast_information'));
$this->setCurrent($xml->xpath('/xml_api_reply/weather/current_conditions'));
} else {
// TODO: retornar erro..
}
return $xml;
}
private function setInformation($data) {
$this->information = $data;
}
public function getInformation() {
return $this->information;
}
private function setCurrent($data) {
$this->current = $data;
}
public function getCurrent() {
return $this->current;
}
private function setForecast($data) {
$this->forecast_list = $data;
}
public function getForecast() {
return $this->forecast_list;
}
public function getHtmlExample() {
$information = $this->getInformation();
$current = $this->getCurrent();
$forecast_list = $this->getForecast();
$html = '
<div class="weather">
<img src="http://www.google.com' . $current[0]->icon['data'] . '" alt="weather" />
<span class="condition">
' . $current[0]->temp_c['data'] . ' °,' .
$current[0]->condition['data'] . '
</span>
</div>
<h3>Previsão</h3>';
foreach ($forecast_list as $forecast) {
$html .='<div class="weather">
<img src="http://www.google.com' . $forecast->icon['data'] . '" alt="weather" />
<span class="condition">' .
$forecast->day_of_week['data'] . ' ' . $forecast->low['data'] . '° - ' . $forecast->high['data'] . '°,
' . $forecast->condition['data'] . '
</span>
</div>';
}
return $html;
}
}
?>