forked from FokkeZB/Meetup
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmeetup.php
More file actions
479 lines (454 loc) · 16.1 KB
/
meetup.php
File metadata and controls
479 lines (454 loc) · 16.1 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
/**
* @package Meetup
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
class Meetup
{
/**
* Base meetup api url
* @const
*/
const BASE = 'https://api.meetup.com';
/**
* Base meetup api url
* @const
*/
const AUTHORIZE = 'https://secure.meetup.com/oauth2/authorize';
/**
* ACCESS meetup api url
* @const
*/
const ACCESS = 'https://secure.meetup.com/oauth2/access';
/**
* GET request
* @const
*/
const GET = 1;
/**
* POST request
* @const
*/
const POST = 2;
/**
* PUT request
* @const
*/
const PUT = 3;
/**
* DELETE request
* @const
*/
const DELETE = 4;
/**
* Parameters for requests
* @var array
*/
protected $_parameters = array();
/**
* The response object from the request
* @var mixed
*/
protected $_response = null;
/**
* Constructor
* @param array $parameters The parameters passed during construction
*/
public function __construct(array $parameters = array())
{
$this->_parameters = array_merge($this->_parameters, $parameters);
$this->_next = $this->_response = null;
}
/**
* Stub for fetching events
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function getEvents(array $parameters = array())
{
return $this->get('/2/events', $parameters);
}
/**
* Stub for fetching groups
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function getGroups(array $parameters = array())
{
return $this->get('/2/groups', $parameters);
}
/**
* Stub for fetching photos
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function getPhotos(array $parameters = array())
{
return $this->get('/2/photos', $parameters);
}
/**
* Stub for fetching discussion boards
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function getDiscussionBoards(array $parameters = array())
{
return $this->get('/:urlname/boards', $parameters);
}
/**
* Stub for fetching discussions
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function getDiscussions(array $parameters = array())
{
return $this->get('/:urlname/boards/:bid/discussions', $parameters);
}
/**
* Stub for fetching member
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function getMembers(array $parameters = array())
{
return $this->get('/2/members', $parameters);
}
/**
* Stub for grabbing the next response data if it's available in the meta information
* of a response. Normally if there's too many results it won't return them all.
*
* @return mixed A json object containing response data
*/
public function getNext()
{
return $this->hasNext() ? $this->api($this->_response->meta->next, array(), self::GET) : null;
}
/**
* Is there more data to retrieve?
*
* @return boolean True if there's more results to process
*/
public function hasNext()
{
$next = null;
if( isset($this->_response->meta) && isset($this->_response->meta->next) )
{
$next = $this->_response->meta->next;
if( strlen($next) )
{
return true;
}
}
return false;
}
/**
* Stub for adding an event
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function postEvent(array $parameters = array())
{
return $this->post('/2/event', $parameters);
}
/**
* Stub for updating an event
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function updateEvent(array $parameters = array())
{
return $this->post('/2/event/:id', $parameters);
}
/**
* Stub for deleting an event
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*/
public function deleteEvent(array $parameters = array())
{
return $this->delete('/2/event/:id', $parameters);
}
/**
* Perform a get on any url supported by meetup, use : to specify parameters that use
* placeholders and pass that exact parameter name as a parameter.
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*
* @code
* $meetup->get('/2/event/:id', array('id'=>10));
* $meetup->get('/2/members', array('group_urlname'=>'foobar'));
* @endcode
*/
public function get($path, array $parameters = array())
{
list($url, $params) = $this->params($path, $parameters);
return $this->api(self::BASE . $url, $params, self::GET);
}
/**
* Perform a post on any url supported by meetup, use : to specify parameters that use
* placeholders and pass that exact parameter name as a parameter.
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*
* @code
* $meetup->post('/2/member/:id', array('id'=>10));
* @endcode
*/
public function post($path, array $parameters = array())
{
list($url, $params) = $this->params($path, $parameters);
return $this->api(self::BASE . $url, $params, self::POST);
}
/**
* Perform a put on any url supported by meetup, use : to specify parameters that use
* placeholders and pass that exact parameter name as a parameter.
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*
* @note There isn't any PUT supported events at the moment
*/
public function put($path, array $parameters = array())
{
list($url, $params) = $this->params($path, $parameters);
return $this->api(self::BASE . $url, $params, self::PUT);
}
/**
* Perform a delete on any url supported by meetup, use : to specify parameters that use
* placeholders and pass that exact parameter name as a parameter.
*
* @param array $parameters The parameters passed for this request
* @return mixed A json object containing response data
* @throws Exception if anything goes wrong
*
* @code
* $meetup->delete('/2/member/:id', array('id'=>10));
* @endcode
*/
public function delete($path, array $parameters = array())
{
list($url, $params) = $this->params($path, $parameters);
return $this->api(self::BASE . $url, $params, self::DELETE);
}
/**
* Utility function for swapping place holders with parameters if any are found in
* the request url. The place holder parameter gets swapped out and the array gets
* the parameter removed, otherwise the request is left un-altered.
*
* @param string $path The relative path of the request from meetup (not including base path)
* @param array $parameters The parameters passed for this request
* @return array An array of the path and parameters modified or un-altered
* @throws Exception if anything goes wrong
*/
protected function params($path, array $parameters = array())
{
$url = $path;
$params = $parameters;
if (preg_match_all('/:([a-z]+)/', $url, $matches))
{
foreach ($matches[0] as $i => $match)
{
if (isset($params[$matches[1][$i]]))
{
$url = str_replace($match, $params[$matches[1][$i]], $url);
unset($params[$matches[1][$i]]);
}
else
{
throw new Exception("Missing parameter '" . $matches[1][$i] . "' for path '" . $path . "'.");
}
}
}
return array($url, $params);
}
/**
* Utility function for authorizing ourselves with meetup. Visit this url
* https://secure.meetup.com/meetup_api/oauth_consumers/ to learn about OATH and the
* consumer details required for authorized access.
*
* @param array $parameters The parameters passed for this request
* @note You're sent to meetup and they will either have an error or a page requiring you to authorize, they'll send
* you back to the redirect uri specified in your consumer details
* @note The parameter 'response_type' is automatically included with value 'code'
*/
public function authorize(array $parameters = array())
{
$location = self::AUTHORIZE . '?' . http_build_query(array_merge($this->_parameters,$parameters, array('response_type'=>'code')));
header("Location: " . $location);
}
/**
* Utility function for getting an access token from meetup with the code they passed back in
* the authorization step. Visit this url https://secure.meetup.com/meetup_api/oauth_consumers/
* to learn about OATH and the consumer details required for authorized access.
*
* @param array $parameters The parameters passed for this request
* @throws Exception if anything goes wrong
* @note The parameter 'grant_type' is automatically included with value 'authorization_code'
*/
public function access(array $parameters = array())
{
return $this->api(self::ACCESS, array_merge($parameters, array('grant_type'=>'authorization_code')), self::POST);
}
/**
* Utility function for getting an refresh token from meetup to avoid authorization from expiring.
* Visit this url https://secure.meetup.com/meetup_api/oauth_consumers/ to learn about OATH and the
* consumer details required for authorized access.
*
* @param array $parameters The parameters passed for this request
* @throws Exception if anything goes wrong
* @note The parameter 'grant_type' is automatically included with value 'refresh_token'
*/
public function refresh(array $parameters = array())
{
return $this->api(self::ACCESS, array_merge($parameters, array('grant_type'=>'refresh_token')), self::POST);
}
/**
* Main routine that all requests go through which handles the CURL call to the server and
* prepares the request accordingly.
*
* @param array $parameters The parameters passed for this request
* @throws Exception if anything goes wrong
* @note The parameter 'sign' is automatically included with value 'true' if using an api key
*/
protected function api($url, $parameters, $action=self::GET)
{
//merge parameters
$params = array_merge($parameters, $this->_parameters);
//make sure 'sign' is included when using api key only
if(in_array('key', $params) && $url!=self::ACCESS && $url!=self::AUTHORIZE)
{
//api request (any) - include sign parameters
$params = array_merge( array('sign', 'true'), $params );
}
//init curl
$ch = curl_init();
$headers = array("Accept-Charset: utf-8");
//set options for connection
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//either GET/POST/PUT/DELETE against api
if($action==self::GET || $action==self::DELETE)
{
//GET + DELETE
//include headers as specified by manual
if( $url == self::ACCESS )
{
array_push($headers, 'Content-Type: application/x-www-form-urlencoded');
}
else if( strpos($url, self::BASE) === 0 && in_array('access_token', $params) )
{
array_merge($params, array('token_type'=>'bearer'));
}
curl_setopt($ch, CURLOPT_URL, $url . (!empty($params) ? ('?' . http_build_query($params)) : ''));
}
else
{
//POST + PUT
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
//need custom types for PUT/DELETE
switch($action)
{
case self::DELETE:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case self::PUT:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//fetch content
$content = curl_exec($ch);
//was there an error on the connection?
if (curl_errno($ch))
{
$error = curl_error($ch);
curl_close($ch);
throw new Exception("Failed retrieving '" . $url . "' because of connection issue: ' " . $error . "'.");
}
//retrieve json and store it internally
$this->_response = json_decode($content);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (!is_null($this->_response) && ($status < 200 || $status >= 300))
{
//tell them what went wrong or just relay the status
if( isset($this->_response->error) && isset($this->_response->error_description) )
{
//what we see against Oath
$error = $this->_response->error . ' - ' . $this->_response->error_description;
}
else if( isset($this->_response->details) && isset($this->_response->problem) && isset($this->_response->code) )
{
//what we see against regular access
$error = $this->_response->code . ' - ' . $this->_response->problem . ' - ' . $this->_response->details;
}
else
{
$error = 'Status ' . $status;
}
throw new Exception("Failed retrieving '" . $url . "' because of ' " . $error . "'.");
}
else if (is_null($this->_response))
{
//did we have any parsing issues for the response?
switch (json_last_error())
{
case JSON_ERROR_NONE:
$error = 'No errors';
break;
case JSON_ERROR_DEPTH:
$error = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = ' Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$error = 'Unknown error';
break;
}
throw new Exception("Cannot read response by '" . $url . "' because of: '" . $error . "'.");
}
return $this->_response;
}
}
?>