This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpetitions_php_sdk.php
320 lines (278 loc) · 8.81 KB
/
petitions_php_sdk.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
<?php
/**
* @file
* Provides SDK for connecting to Petitions API resources.
*
* @version 0.1
*/
class PetitionsPhpSdkApiConnector {
protected $apiHost = NULL;
protected $apiKey = NULL;
protected $allowInsecure = FALSE;
/**
* Class Constructor.
*
* @param string $base
* The API host base URL, e.g., https://example.com.
* @param string $key
* The API key.
* @param bool $allow_insecure
* (optional) Whether or not to allows insecure SSL connection. TRUE to
* allow. Defaults to FALSE.
*/
public function __construct($base, $key, $allow_insecure = FALSE) {
$this->apiHost = $base;
$this->apiKey = $key;
$this->allowInsecure = (bool) $allow_insecure;
// This URL is used to test the connection.
$test_url = "petitions.json";
$this->runCurl($test_url);
}
/**
* General cURL request function for GET and POST.
*
* @param string $url
* URL to be requested.
*
* @param array $get_vals
* (optional) Array of GET values. Defaults to an empty array.
*
* @param array $post_vals
* (optional) String to be sent with POST request. Defaults to NULL.
*
* @return object
* The decoded JSON object.
*/
protected function runCurl($url, $get_vals = array(), $post_vals = NULL) {
// Prepend apiHost URL.
$url = $this->apiHost . '/' . $url;
// Add $_GET params.
if ($this->apiKey) {
$get_vals = array_merge($get_vals, array('api_key' => $this->apiKey));
}
$get_string = $this->buildQueryString($get_vals);
$url .= (strpos($url, '?') !== FALSE ? '&' : '?') . $get_string;
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 3,
);
if ($this->allowInsecure) {
$options[CURLOPT_SSL_VERIFYPEER] = FALSE;
$options[CURLOPT_SSL_VERIFYHOST] = FALSE;
}
if ($post_vals != NULL) {
$post_string = json_encode($post_vals);
$options[CURLOPT_CUSTOMREQUEST] = "POST";
$options[CURLOPT_POSTFIELDS] = $post_string;
$options[CURLOPT_HTTPHEADER] = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post_string),
);
}
curl_setopt_array($ch, $options);
$response = json_decode(curl_exec($ch));
curl_close($ch);
// This can be overridden so that exceptions are caught in a child class.
$this->verifyResponse($response, $url);
return $response;
}
/**
* Verifies that a response was successful.
*
* Overriding this method provides an opportunity for custom error logging.
*
* @param object $response
* The response obtained from the API.
*
* @param string $url
* The URL of the curl request.
*
* @throws PetitionsPhpSdkConnectionException
* @throws PetitionsPhpSdkResponseServerException
* @throws PetitionsPhpSdkResponseClientException
*/
protected function verifyResponse(&$response, $url) {
if (empty($response->metadata->responseInfo->status)) {
$e = new PetitionsPhpSdkConnectionException("Could not connect to Petitions API.");
$e->response = $response;
$e->requestUrl = $url;
throw $e;
}
elseif ($response->metadata->responseInfo->status != 200) {
$developer_message = $response->metadata->responseInfo->developerMessage;
// Distinguish between server errors and client errors.
if ($response->metadata->responseInfo->status == 500) {
$e = new PetitionsPhpSdkResponseServerException("Petitions API returned an Error code: " . $developer_message);
}
else {
$e = new PetitionsPhpSdkResponseClientException("Petitions API returned an Error code: " . $developer_message);
}
$e->response = $response;
$e->requestUrl = $url;
throw $e;
}
}
/**
* Parses an array into a valid, rawurlencoded query string.
*
* This differs from http_build_query() as we need to rawurlencode() (instead
* of urlencode()) all query parameters.
*
* @param array $query
* The query parameter array to be processed, e.g. $_GET.
*
* @param string $parent
* (optional) Internal use only. Used to build the $query array key for
* nested items. Defaults to empty string.
*
* @return string
* A rawurlencoded string which can be used as or appended to the URL query
* string.
*
* @see drupal_http_build_query()
*/
public function buildQueryString(array $query, $parent = '') {
$params = array();
foreach ($query as $key => $value) {
$key = ($parent ? $parent . '[' . rawurlencode($key) . ']' : rawurlencode($key));
// Recurse into children.
if (is_array($value)) {
$params[] = $this->buildQueryString($value, $key);
}
// If a query parameter value is NULL, only append its key.
elseif (!isset($value)) {
$params[] = $key;
}
else {
// For better readability of paths in query strings, we decode slashes.
$params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
}
}
return implode('&', $params);
}
/**
* Fetches a list of petitions.
*
* @param int $limit
* (optional) The maximum number of results to return. Defaults to 10.
*
* @param int $offset
* (optional) The offset of the resultset to return. Defaults to 0.
*
* @param array $parameters
* (optional) An associative array of $_GET parameters to be appended to the
* request. Defaults to an empty array.
*
* @return object
* The JSON response.
*/
public function getPetitions($limit = 10, $offset = 0, $parameters = array()) {
$resource = 'petitions.json';
$get_vals = array(
'limit' => $limit,
'offset' => $offset,
);
$get_vals += $parameters;
return $this->runCurl($resource, $get_vals);
}
/**
* Fetches a specific petition.
*
* @param string $petition_id
* The ID of the petition to fetch.
*
* @param bool $mock
* Indicate whether returned data should be mock data (not real).
*
* @return object
* The JSON response.
*/
public function getPetition($petition_id, $mock = FALSE) {
$resource = 'petitions/' . $petition_id . '.json';
$get_vals = array();
if ($mock) {
$get_vals['mock'] = '1';
}
return $this->runCurl($resource, $get_vals);
}
/**
* Fetches a list of signatures for a specific petition.
*
* @param string $petition_id
* The ID of the petition to fetch.
*
* @param int $limit
* (optional) The maximum number of results to return. Defaults to 10.
*
* @param int $offset
* (optional) The offset of the resultset to return. Defaults to 0.
*
* @param array $parameters
* (optional) An associative array of $_GET parameters to be appended to the
* request. Defaults to empty array.
*
* @return object
* The JSON response.
*
* @see https://petitions.whitehouse.gov/developers
*/
public function getSignatures($petition_id, $limit = 10, $offset = 0, $parameters = array()) {
$resource = 'petitions/' . $petition_id . '/signatures.json';
$get_vals = array(
'limit' => $limit,
'offset' => $offset,
);
$get_vals += $parameters;
return $this->runCurl($resource, $get_vals);
}
/**
* Send a signature to Petitiosn API.
*
* @param array $signature
* An associative array. Verify that correct signature keys
* are included, as per development documentation.
*
* @return object
* The JSON response.
*
* @see https://petitions.whitehouse.gov/developers
*/
public function sendSignature($signature) {
$resource = 'signatures.json';
return $this->runCurl($resource, array(), $signature);
}
/**
* Fetches validated signatures.
*
* @param string $petition_id
* (optional) The id of the petition for which validations will be fetched.
* Defaults to NULL.
*
* @param int $limit
* (optional) The maximum number of results to return. Defaults to 10.
*
* @param int $offset
* (optional) The offset of the resultset to return. Defaults to 0.
*
* @return object
* The JSON response.
*/
public function getValidations($petition_id = NULL, $limit = 10, $offset = 0) {
$get_vals = array(
'key' => $this->apiKey,
'limit' => $limit,
'offset' => $offset,
);
if ($petition_id) {
$get_vals['petition_id'] = $petition_id;
}
$resource = 'validations.json';
return $this->runCurl($resource, $get_vals);
}
}
class PetitionsPhpSdkException extends Exception {}
class PetitionsPhpSdkConnectionException extends PetitionsPhpSdkException {}
class PetitionsPhpSdkResponseException extends PetitionsPhpSdkException {}
class PetitionsPhpSdkResponseServerException extends PetitionsPhpSdkResponseException {}
class PetitionsPhpSdkResponseClientException extends PetitionsPhpSdkResponseException {}