-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaasApi.php
189 lines (166 loc) · 5.25 KB
/
MaasApi.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php // MaasApi.php v0.1.0
/**
* The MaasApi class is an attempt to allow PHP developers
* a way to easily gather data from the {MAAS} API.
*
* More info on the {MAAS} API can be found at http://marsweather.ingenology.com/
*
* @author Dan Bough <[email protected]> http://www.danielbough.com
* @copyright Copyright (C) 2010-2013
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
class MaasApi {
/**
* Latest data URL.
* @var string
*/
var $latestUrl = "http://marsweather.ingenology.com/v1/latest/";
/**
* Archive data URL.
* @var string
*/
var $archiveUrl = "http://marsweather.ingenology.com/v1/archive/";
/**
* Raw JSON object with latest data.
* @return string
*/
public function getLatestRaw()
{
return file_get_contents($this->latestUrl);
}
/**
* Latest data as JSON
* @return string
*/
public function getLatestJson()
{
return json_encode($this->getLatest());
}
/**
* Array of latest data.
* @return array
*/
public function getLatest()
{
$jsonData = file_get_contents($this->latestUrl);
$this->report = json_decode($jsonData)->report;
return $this->report;
}
/**
* Raw JSON object with first page of archived data.
* @return string
*/
public function getArchiveRaw()
{
return file_get_contents($this->archiveUrl);
}
/**
* JSON object of all archive data.
* @return array
*/
public function getArchiveJson()
{
return json_encode($this->getArchiveAll());
}
/**
* Array of all archive data.
* @return array
*/
public function getArchiveAll()
{
$jsonData = file_get_contents($this->archiveUrl);
$data = json_decode($jsonData);
return $this->get($data);
}
/**
* Get archive data based on search parameters.
* All properties of the object returned by the {MAAS} API can be searched for.
* You can also provide a date range with terrestrial_date_start and terrestrial_date_end.
* See more info at http://marsweather.ingenology.com/
* @param array $params
* @return array
*/
public function getArchiveSearch($params)
{
/*
Example $params:
$params = array(
"terrestrial_date_start"=>"2013-05-01",
"terrestrial_date_end"=>"2013-05-10"
);
*/
$urlSuffix = "?";
$count = count($params);
foreach ($params as $key => $val) {
$urlSuffix .= $key . "=" . urlencode($val);
// Unless we're at the last param, add a & separator
if ($count != 1) {
$urlSuffix .="&";
}
$count--;
}
$jsonData = file_get_contents($this->archiveUrl . $urlSuffix);
$data = json_decode($jsonData);
return $this->get($data, substr($urlSuffix, 1));
}
/**
* Uses first page results of archive query to gather all data from that query.
* @param object $data
* @return array
*/
public function get($data, $urlSuffix=NULL)
{
$results = $data->results;
// Determine how many pages of data there are
if ($data->count > 10) {
$pages = ceil($data->count / 10);
}
// Context for ignoring HTTP / PHP errors with file_get_contents
$context = stream_context_create(array(
'http' => array('ignore_errors' => true),
));
// Start at page 2 and go to the end
for ($i=2;$i<=$pages;$i++) {
$jsonData = file_get_contents($this->archiveUrl . "?page=" . $i . "&" . $urlSuffix, false, $context);
// If $jsonData is an object add it's data to our results array.
if ($this->isJson($jsonData)) {
foreach (json_decode($jsonData)->results as $result) {
array_push($results, $result);
}
}
}
return $results;
}
/**
* Determines whether or not a string is a JSON object
* Since this requires PHP 5.3, I'm assuming the JSON is OK
* for any version below that. Probably a better way of doing this
* but I'll worry about that later.
* @param string $string
* @return boolean
*/
private function isJson($string) {
$version = substr(phpVersion(), 0, 3);
if ($version >= "5.3") {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
else {
return true;
}
}
}