-
Notifications
You must be signed in to change notification settings - Fork 15
/
MeetupConnection.class.php
156 lines (137 loc) · 4.98 KB
/
MeetupConnection.class.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
<?php
/**
* The collection of parameters needed to make requests against the Meetup API.
* Currently only supports api key authentication, but can be extended to
* support oAuth.
*/
abstract class MeetupConnection {
abstract protected function modify_params( $Parameters );
/**
* Performs the GET query against the specified endpoint
*
* @throws MeetupBadRequestException
* @throws MeetupUnauthorizedRequestException
* @throws MeetupInternalServerErrorException
* @param String $Url - Endpoint with URL paramters (for now)
* @return MeetupApiResponse
*/
public function get( $Url ) {
// Clear error status
$ret = false;
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_USERAGENT => "PHP Meetup.com API Client", // who am i
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_HEADER => 0
);
$ch = curl_init( $Url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['content'] = $content;
$response = new MeetupApiResponse();
$response->setHttpCode($header['http_code']);
$response->setResponse($header['content']);
if( $response->getHttpCode() == '400' ) {
// 400 Bad request when there was a problem with the request
throw new MeetupBadRequestException($Url, $response);
} else if ( $response->getHttpCode() == '401' ) {
// 401 Unauthorized when you don't provide a valid key
throw new MeetupUnauthorizedRequestException();
} else if ( $response->getHttpCode() == '500' ) {
// 500 Internal Server Error
throw new MeetupInternalServerErrorException();
}
return $response;
}
/**
*
* @throws MeetupInvalidParametersException
* @param <type> $Endpoint
* @param <type> $Parameters
* @param <type> $RequiredParameters - Optional - Some endpoints do not have parameters
* @return <type>
*/
public function buildUrl( $Endpoint, $Parameters, $RequiredParameters = null ) {
if(is_array($RequiredParameters) && !$this->verifyParameters( $RequiredParameters, $Parameters )) {
throw new MeetupInvalidParametersException( $RequiredParameters );
}
$Parameters = $this->modify_params($Parameters);
$params = '';
foreach($Parameters AS $k => $v) {
$params .= "$k=$v&";
}
rtrim($params,'&');
return MEETUP_API_URL . $Endpoint . "?" . $params;
}
/**
* Checks the input parameters against a list of required parameters to
* ensure at least one of the required parameters exists.
*
* NOTE: The Meetup API contains a list of parameters that are required for
* each endpoint with a default condition of "any of"
*
* @param Array $RequiredList - Names of required parameters
* @param Array $Parameters - List of provided paramters
* @return Boolean
*/
public function verifyParameters($RequiredList, $Parameters) {
$Parameters = array_keys($Parameters);
/*
* Check to see if any of the required list is in the parameters array
* Since the Meetup API requires "any of" if a required key is found in
* parameters the verification will pass
*/
foreach($RequiredList AS $r) {
if(in_array($r, $Parameters)) {
return true;
}
}
return false;
}
}
class MeetupKeyAuthConnection extends MeetupConnection {
private $_key;
/*
* Initializes a connection to the Meetup API using API keys
*
* @param String $key - A users's Meetup api key
*/
public function __construct($key) {
$this->_key = $key;
}
/**
* Adds additional query parameters for key authentication
*
* @param Array $params - request parameters
* @return Array modified request parameters
*/
public function modify_params($params) {
$params['key'] = $this->_key;
return $params;
}
}
class MeetupOAuth2Connection extends MeetupConnection {
private $_access_token;
/*
* Initializes a connection to the Meetup API using oAuth 2
*
* @param String $access_token - A valid access token received from a Meetup access token request
*/
public function __construct($access_token) {
$this->_access_token = $access_token;
}
/**
* Adds additional query parameters for key authentication
*
* @param Array $params - request parameters
* @return Array modified request parameters
*/
public function modify_params($params) {
$params['access_token'] = $this->_access_token;
return $params;
}
}