-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_weather.php
71 lines (61 loc) · 2.13 KB
/
google_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* @package PyroCMS
* @subpackage Google Weather Widget
* @author Gustavo Rodriguez Baldera - http://twitter.com/gbaldera
*
* Show Weather in your site
*/
class Widget_Google_weather extends Widgets
{
public $title = 'Google Weather';
public $description = array(
'en' => 'Show weather from Google Weather\'s API',
'es' => 'Muestra el clima desde el API de Google Weather'
);
public $author = 'Gustavo Rodriguez Baldera';
public $website = 'http://twitter.com/gbaldera';
public $version = '1.0';
public $fields = array(
array(
'field' => 'location',
'label' => 'Location',
'rules' => 'required'
),
array(
'field' => 'lang',
'label' => 'Language',
'rules' => ''
),
);
public function run($options)
{
if (empty($options['lang']))
{
$options['lang'] = 'en';
}
//caching
if (!$content = $this->pyrocache->get('weather/' . md5('weather-' . url_title($options['location']))))
{
$city = urlencode($options['location']);
$content = utf8_encode(file_get_contents('http://www.google.com/ig/api?weather=' . $city . '&hl=' . $options['lang']));
//write cache and expires in 12 hours
$this->pyrocache->write($content, 'weather/' . md5('weather-' . url_title($options['location'])), 43200);
}
$xml = simplexml_load_string($content);
// Fail?
if (!$xml)
{
echo 'Error trying to contact the Google Weather server.';
return false;
}
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
return array(
'information' => $information,
'current' => $current,
'forecast_list' => $forecast_list
);
}
}