-
Notifications
You must be signed in to change notification settings - Fork 4
/
MeetupAPIBase.php
322 lines (291 loc) · 10.9 KB
/
MeetupAPIBase.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* @file MeetupAPIBase.php
* @author Kevin Kaland <help [at] wizonesolutions [dot] com>
* This file contains methods common to all client classes and serves as the base class, providing properties that child classes should override.
*/
/**
* MeetupAPIBase
* This class contains the essential functionality to make building out the API functionality easier. It can also be implemented itself. In this case, it needs to be supplied the method for the API request.
* @package MeetupAPI
* @version $id$
* @copyright 2010 WizOne Solutions
* @author Kevin Kaland <help [at] wizonesolutions [dot] com>
* @license GNU Public License Version 2.0
*/
class MeetupAPIBase {
protected $validFormats = array('json',
'json-alt',
);
protected $apiKey, $apiUrl, $format, $method, $pageSize, $numPages, $sortDesc, $query, $curl; //This should be set to the Meetup API method implemented by the child class.
function __construct($apiKey, $method) {
require_once(dirname(__FILE__) . '/config.php');
$this->apiKey = $apiKey;
$this->method = $method;
$this->apiUrl = API_URL;
$this->format = API_FORMAT;
if ($this->format == 'json') {
//Choose the (hopefully) best json library available based on the PHP version
if (version_compare(phpversion(), '5.3') >= 0) {
//Do nothing
}
else {
//Use the JSON functionality that the XML-RPC library provides
$this->format = 'json-alt';
}
}
$this->pageSize = API_PAGE_SIZE;
$this->numPages = API_NUM_PAGES;
$this->sortDesc = API_SORT_DESC;
}
function __autoload($className) {
require_once(dirname(__FILE__) . $className . '.php');
}
function setFormat($format) {
if (in_array($format, $this->validFormats)) $this->format = $format;
}
function setQuery($query) {
$this->validateQuery($query);
}
/**
* validateQuery
* In child classes, this should be overridden and strip out invalid parameters, etc.
* @param mixed $query
* @return void
*/
protected function validateQuery($query) {
$this->query = $query;
}
protected function validateFormat() {
if (!in_array($this->format, $this->validFormats)) return FALSE;
else return TRUE;
}
function getResponse($offset = 0, $responseData = NULL) {
if (!$this->validateFormat()) return FALSE;
if (!isset($responseData)) {
$responseData = $this->request($offset);
}
if ($pageResponse = $response = $this->parseResponse($responseData)) {
$pagesRead = 1;
$currOffset = $offset;
//The Meetup API gives us a handy, dandy next link! Let's use that! I feel like I'm using linked lists again!
$done = FALSE;
while ($done != TRUE) {
$nextRequestUrl = $this->getNextRequestUrl($pageResponse);
//Are we out of stuff we need to fetch?
if ( ($pagesRead == $this->numPages) || empty($nextRequestUrl) || ($this->pageSize * ($currOffset + 1)) > $this->getMeta($pageResponse, 'total_count') ) {
$done = TRUE;
continue;
}
else {
//Keep going
$pageResponseData = $this->request(0, $nextRequestUrl); //Wonder if I'll ever actually use the "offset" feature of MeetupAPIBase::request...?
//Overwrite the metadata and merge the results
if (!$pageResponse = $this->parseResponse($pageResponseData)) {
break; //Exit cleanly (I hope) if an unexpected problem occurs
}
$response->meta = $pageResponse->meta;
$response->results = array_merge($response->results, $pageResponse->results);
$pagesRead++;
$currOffset++;
}
}
return $response;
}
else return FALSE;
}
function getRawResponse($offset = 0) {
if (!$this->validateFormat()) return FALSE;
return $this->request($offset);
}
protected function getMeta($response, $field) {
switch($this->format) {
case 'json':
case 'json-alt':
return $response->meta->$field;
break;
default:
return FALSE;
break;
}
}
protected function getPagingRequest($response, $direction = 'next') {
return $this->getMeta($response, $direction);
}
protected function getNextRequestUrl($response) {
return $this->getPagingRequest($response);
}
protected function getPrevRequestUrl($response) {
return $this->getPagingRequest($response, 'prev');
}
function parseResponse($responseData) {
switch($this->format) {
case 'json':
//This json_decode has BIG limitations in PHP 5.2, but it is built into PHP and is thus preferred if a decent one is available. json-alt is chosen if the PHP version isn't at least 5.3.
$jsonResponse = json_decode($responseData);
return $jsonResponse;
break;
case 'json-alt':
require_once(dirname(__FILE__) . '/libraries/xmlrpc/lib/xmlrpc.inc');
require_once(dirname(__FILE__) . '/libraries/xmlrpc/extras/jsonrpc/jsonrpc.inc');
require_once(dirname(__FILE__) . '/libraries/xmlrpc/extras/jsonrpc/json_extension_api.inc');
$jp = 'json_';
if (in_array('json', get_loaded_extensions())) {
$jp = 'json_alt_';
}
$jsonDecode = $jp . 'decode';
$jsonLastError = $jp . 'last_error'; //TODO: Actually handle this
$jsonResponse = $jsonDecode($responseData, FALSE, 1);
// @todo MEDIUM: Do something with this to provide error messages, at least for JSON
/* switch(json_alt_last_error()) {
case JSON_ALT_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ALT_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ALT_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ALT_ERROR_NONE:
echo ' - No errors';
} */
return $jsonResponse;
break;
case 'xml':
// @todo MEDIUM: Fix problems with XML parsing...disabling for now
/*$xml_response_raw = simplexml_load_string($responseData);
//Here goes nothing
$xml_response = $this->parseXmlIntoObject($xml_response_raw);
return $xml_response;*/
break;
default:
//We don't know how to execute this request, so return FALSE.
// @todo MEDIUM: Add some sort of notification or warning to the user that this format is not yet supported.
return FALSE;
break;
}
}
/* @todo LOW: Taking this XML at face value is a bad idea. It would be better to hard-map the tags and data hierarchy to expect, a bit like I do in Easy Populate Converter. Then I can traverse it without recursion and get around the hideous question of, "How do I tell if a child has children itself, and if those are part of a list of itself or simply child properties?" Granted, there are few child properties that actually themselves contain multiple results. In fact, I can only think of topics. So accomodating all children isn't really needed. I can abstract this a bit and make life easier. I can refactor it later.
THEREFORE, this XML parsing is probably not something that MeetupAPIBase should be doing at all. It might work, though...need to play with it a bit more.
A bit to consider.
*/
protected function parseXmlIntoObject($xmlObj, $depth = 0) {
$xmlResult = new stdClass();
$valueCount = count($xmlObj);
if ($valueCount) {
foreach($xmlObj as $key => $value) {
$useKey = $key;
$children = $value->children();
if (count($children)) {
foreach($children as $key2 => $value2) {
$useKey2 = $key2;
if (isset($children->{$useKey2}[1])) {
$arrayMode = TRUE;
}
if ($arrayMode) {
$xmlResult->{$useKey}[] = $this->parseXmlIntoObject($value2, $depth + 1);
}
else {
$xmlResult->{$useKey}->{$useKey2} = $this->parseXmlIntoObject($value2, $depth + 1);
}
}
}
else {
//No children, so use the value itself - put it straight into xmlResult if we're still on depth 0, otherwise return it
if ($depth == 0) {
$xmlResult->{$useKey} = $value;
}
else {
return (string) $value;
}
}
if ($depth > 0) return $xmlResult;
}
}
else {
if ($depth == 0) return FALSE;
else return (string) $xmlObj;
}
if ($depth == 0) return $xmlResult;
}
/**
* initRequest
* Wrapper function to initiate the HTTP request.
* @return void
*/
protected function initRequest($offset = 0, $request = NULL) {
if (!isset($request)) {
//URL-encode the query
$request_settings = array('key' => $this->apiKey,
'page' => $this->pageSize,
'offset' => $offset,
'desc' => $this->sortDesc,
);
if ($this->sortDesc == NULL) unset($request_settings['desc']); // Don't put desc in the query string at all if it isn't being used; its very presence sorts results descending!
$request = $this->getRequestUrl() . '?' . http_build_query(array_merge($this->query, $request_settings));
}
$this->curl = curl_init($request);
curl_setopt($this->curl, CURLOPT_HEADER, FALSE);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
}
/**
* execRequest
*
* @return stdClass
*/
protected function execRequest() {
// json_decode cannot handle very large numbers. So the UTC time is shortened.
$responseData = preg_replace("/(\d{10})000,/",'$1,',curl_exec($this->curl));
return $responseData;
}
/**
* closeRequest
* Wrapper function to close the HTTP request.
* @return void
*/
protected function closeRequest() {
curl_close($this->curl);
}
protected function request($offset = 0, $request = NULL) {
$this->initRequest($offset, $request);
$responseData = $this->execRequest();
$this->closeRequest();
return $responseData;
}
protected function getRequestUrl() {
return $this->apiUrl . $this->method . '.' . $this->formatRequestName($this->format);
}
/**
* formatRequestName
* When various implementations of a format exist, this helper function ensures the right name is still passed to the API.
* @param mixed $formatName
*/
protected function formatRequestName($formatName) {
switch($formatName) {
case 'json-alt':
return 'json';
default:
return $formatName;
}
}
function setPageSize($pageSize) {
if ((int) $pageSize > -1) $this->pageSize = (int) $pageSize;
}
function getPageSize() {
return $this->pageSize;
}
function setNumPages($numPages) {
if (is_numeric($numPages) && (int) $numPages >= 0) $this->numPages = $numPages;
}
function getNumPages() {
return $this->numPages;
}
function setSortDesc($sortDesc) {
if ($sortDesc == TRUE) $this->sortDesc = 'true';
else $this->sortDesc = NULL;
}
function getSortDesc() {
return $this->sortDesc;
}
}