-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meetup.php
120 lines (94 loc) · 3.24 KB
/
Meetup.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
<?php
//for ssl use https else http
class Meetup {
const BASE = 'https://api.meetup.com';
protected $_parameters = array(
'sign' => 'true',
);
public function __construct(array $parameters = array()) {
$this->_parameters = array_merge($this->_parameters, $parameters);
}
public function getEvents(array $parameters = array()) {
return $this->get('/2/events', $parameters)->results;
}
public function getEventsbyId(array $parameters = array()) {
return $this->get('/2/event/:id', $parameters);
}
public function getPhotos(array $parameters = array()) {
return $this->get('/2/photos', $parameters)->results;
}
public function getComments(array $parameters = array()) {
return $this->get('/2/event_comments', $parameters)->results;
}
public function getDiscussionBoards(array $parameters = array()) {
return $this->get('/:urlname/boards', $parameters);
}
public function getDiscussions(array $parameters = array()) {
return $this->get('/:urlname/boards/:bid/discussions', $parameters);
}
public function getEventsMeta(array $parameters = array()) {
return $this->get('/2/events', $parameters)->meta;
}
public function get($path, array $parameters = array()) {
$parameters = array_merge($this->_parameters, $parameters);
if (preg_match_all('/:([a-z]+)/', $path, $matches)) {
foreach ($matches[0] as $i => $match) {
if (isset($parameters[$matches[1][$i]])) {
$path = str_replace($match, $parameters[$matches[1][$i]], $path);
unset($parameters[$matches[1][$i]]);
} else {
throw new Exception("Missing parameter '" . $matches[1][$i] . "' for path '" . $path . "'.");
}
}
}
$url = self::BASE . $path . '?' . http_build_query($parameters);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Charset: utf-8"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("Failed retrieving '" . $url . "' because of ' " . $error . "'.");
}
$response = json_decode($content);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status != 200) {
if (isset($response->errors[0]->message)) {
$error = $response->errors[0]->message;
} else {
$error = 'Status ' . $status;
}
throw new Exception("Failed retrieving '" . $url . "' because of ' " . $error . "'.");
}
if (isset($response) == false) {
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 $response;
}
}