-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherline
executable file
·127 lines (101 loc) · 3.55 KB
/
weatherline
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/php
<?php
if (PHP_SAPI !== 'cli')
{
die('this is a CLI script.');
}
//http://www.google.com/ig/api?weather=leusden
$curl = curl_init();
if(!$curl) die("can't open curl handle");
$ehader = array();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
$header[] = "Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt_array($curl, array(
CURLOPT_URL => "http://www.google.com/ig/api?weather=leusden",
CURLOPT_USERAGENT => 'Googlebot/2.1 (+http://www.google.com/bot.html)',
CURLOPT_HTTPHEADER => $header,
CURLOPT_REFERER =>'http://www.google.com',
CURLOPT_ENCODING => 'gzip,deflate',
CURLOPT_AUTOREFERER => true,
CURLOPT_CRLF => true,
CURLOPT_FILETIME => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_FORBID_REUSE => true,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 20,
));
$file =curl_exec($curl);
$curl_errno = curl_errno($curl);
$curl_error = curl_error($curl);
curl_close($curl);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
die;
}
if(!$file) return;
$xml = simplexml_load_string($file);
if(!$xml) return;
$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");
/*
$output = `echo "$temp, \033[0;36m$cond\033[0m, ZICHT:\033[0;36m$zicht\033[0m, LICHT:\033[0;36m$zon\033[0m TOT \033[0;36m$maan\033[0m";
echo "DRUK:\033[0;36m$druk\033[0m, VOCHTIGHEID:\033[0;36m$lucht\033[0m, WIND:\033[0;36m$wind\033[0m"`;
*/
//echo xmlpp($file);
echo 'Nu: ';
echo $current[0]->temp_c['data'], '°C, ', $current[0]->condition['data'],', ', $current[0]->humidity['data'], PHP_EOL;
foreach($forecast_list as $forecast) {
echo $forecast->day_of_week['data'], ': ';
echo $forecast->low['data'], '°C / ';
echo $forecast->high['data'], '°C, ';
echo $forecast->condition['data'], '.', PHP_EOL;
}
if (!function_exists('curl_setopt_array')) {
function curl_setopt_array(&$ch, $curl_options)
{
foreach ($curl_options as $option => $value) {
if (!curl_setopt($ch, $option, $value)) {
return false;
}
}
return true;
}
}
function xmlpp($xml, $html_output=false) {
$xml_obj = new SimpleXMLElement($xml);
$level = 4;
$indent = 0; // current indentation level
$pretty = array();
// get an array containing each XML element
$xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));
// shift off opening XML tag if present
if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
$pretty[] = array_shift($xml);
}
foreach ($xml as $el) {
if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
// opening tag, increase indent
$pretty[] = str_repeat(' ', $indent) . $el;
$indent += $level;
} else {
if (preg_match('/^<\/.+>$/', $el)) {
$indent -= $level; // closing tag, decrease indent
}
if ($indent < 0) {
$indent += $level;
}
$pretty[] = str_repeat(' ', $indent) . $el;
}
}
$xml = implode("\n", $pretty);
return ($html_output) ? htmlentities($xml) : $xml;
}