summaryrefslogtreecommitdiff
path: root/protected/extensions/wetter/Wetter.php
blob: de2ec93bfbfff0d8ffb5eb96443f27d072f996cb (plain)
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
<?php
class Wetter extends CWidget {
	
	public function run(){
		$wetter = $this->website_wetter('71332','Germany');
		$this->render('wetter', compact('wetter'));
	}
	
	/*
	 Nutzung dieses Scripts nur gestattet, wenn Kommentare in PHP nicht entfernt werden oder ein Link zu folgender Adresse gesetzt wird:
	URL: http://www.web-spirit.de/webdesign-tutorial/9/Wetter-auf-eigener-Website-mit-Google-Weahter-API
	Beschreibung: Wettervorhersage auf der eigenen Website mit Zugriff auf die Google Weather API
	Autor: Sebastian Gollus
	Internet: http://www.web-spirit.de
	Version: 1.0.201106
	*/
	
	// Funktionsaufruf z.B.: $wetter = website_wetter("46562", "Germany", "/images/wetter/icons/", "de");
	
	private function website_wetter($plz, $land, $icons_src="/", $sprache="de", $ort="")
	{
		$icons_google = "/ig/images/weather/"; //alte Google-Version
		//$icons_google = "http://g0.gstatic.com/images/icons/onebox/"; //neue Google-Version (bzw. zwischenzeitlich gendert)
	
		if($ort != "")
		{
			$station = $ort;
		}
		else
		{
			$station = $plz."-".$land;
		}
	
		$api = simplexml_load_string(utf8_encode(file_get_contents("http://www.google.com/ig/api?weather=".$station."&hl=".$sprache)));
	
		$wetter = array();
	
		// Allgemeine Informationen
		$wetter['stadt'] = $api->weather->forecast_information->city->attributes()->data;
		$wetter['datum'] = $api->weather->forecast_information->forecast_date->attributes()->data;
		$wetter['zeit'] = $api->weather->forecast_information->current_date_time->attributes()->data;
	
		// Aktuelles Wetter
		$wetter[0]['zustand'] = $api->weather->current_conditions->condition->attributes()->data;
		$wetter[0]['temperatur'] = $api->weather->current_conditions->temp_c->attributes()->data;
		$wetter[0]['luftfeuchtigkeit'] = $api->weather->current_conditions->humidity->attributes()->data;
		$wetter[0]['wind'] = $api->weather->current_conditions->wind_condition->attributes()->data;
		$wetter[0]['icon'] = str_replace($icons_google, $icons_src, $api->weather->current_conditions->icon->attributes()->data);
	
		// Wettervorhersage heute, morgen, in zwei und in drei Tagen ($wetter[1] bis $wetter[4])
		$i = 1;
		foreach($api->weather->forecast_conditions as $weather)
		{
			$wetter[$i]['wochentag'] = $weather->day_of_week->attributes()->data;
			$wetter[$i]['zustand'] = $weather->condition->attributes()->data;
			$wetter[$i]['tiefsttemperatur'] = $weather->low->attributes()->data;
			$wetter[$i]['hoechsttemperatur'] = $weather->high->attributes()->data;
			$wetter[$i]['icon'] = str_replace($icons_google, $icons_src, $weather->icon->attributes()->data);
	
			$i++;
		}
	
		return $wetter;
	}
}