1 | <?php |
||
28 | class UrlQuery |
||
29 | { |
||
30 | /** |
||
31 | * The default protocol the Soda API expects |
||
32 | */ |
||
33 | const DEFAULT_PROTOCOL = "https"; |
||
34 | |||
35 | /** |
||
36 | * The API endpoint that will be used in all requests |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $url; |
||
41 | |||
42 | /** |
||
43 | * The cURL object this class is a wrapper for |
||
44 | * |
||
45 | * @var resource |
||
46 | */ |
||
47 | private $cURL; |
||
48 | |||
49 | /** |
||
50 | * The Socrata API token |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | private $token; |
||
55 | |||
56 | /** |
||
57 | * HTTP headers sent in all requests |
||
58 | * |
||
59 | * @var string[] |
||
60 | */ |
||
61 | private $headers; |
||
62 | |||
63 | /** |
||
64 | * The OAuth 2.0 token sent in all requests |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $oAuth2Token; |
||
69 | |||
70 | /** |
||
71 | * Configure all of the authentication needed for cURL requests and the API endpoint |
||
72 | * |
||
73 | * **Note** If OAuth 2.0 is used for authentication, do not give values to the $email and $password parameters; |
||
74 | * instead, use the `setOAuth2Token()` function. An API token will still be required to bypass throttling. |
||
75 | * |
||
76 | * @param string $url The API endpoint this instance will be calling |
||
77 | * @param string $token The API token used in order to bypass throttling |
||
78 | * @param string $email The email address of the user being authenticated through Basic Authentication |
||
79 | * @param string $password The password for the user being authenticated through Basic Authentication |
||
80 | * |
||
81 | * @see setOAuth2Token |
||
82 | * |
||
83 | * @since 0.1.0 |
||
84 | */ |
||
85 | public function __construct ($url, $token = "", $email = "", $password = "") |
||
100 | |||
101 | /** |
||
102 | * Clean up after ourselves; clean up the cURL object. |
||
103 | */ |
||
104 | public function __destruct () |
||
108 | |||
109 | /** |
||
110 | * Set the OAuth 2.0 token that requests will be using. This function does **not** retrieve a token, it simply uses |
||
111 | * the existing token and sends it as authentication. |
||
112 | * |
||
113 | * @param string $token The OAuth 2.0 token used in requests |
||
114 | * |
||
115 | * @since 0.1.2 |
||
116 | */ |
||
117 | public function setOAuth2Token ($token) |
||
125 | |||
126 | /** |
||
127 | * Send a GET request |
||
128 | * |
||
129 | * @param string $params The GET parameters to be appended to the API endpoint |
||
130 | * @param bool $associativeArray When true, the returned data will be associative arrays; otherwise, it'll be an |
||
131 | * StdClass object. |
||
132 | * @param array $headers An array where the return HTTP headers will be stored |
||
|
|||
133 | * |
||
134 | * @see SodaClient::enableAssociativeArrays |
||
135 | * |
||
136 | * @since 0.1.0 |
||
137 | * |
||
138 | * @return mixed An associative array matching the returned JSON result or an StdClass object |
||
139 | */ |
||
140 | public function sendGet ($params, $associativeArray, &$headers = NULL) |
||
160 | |||
161 | /** |
||
162 | * Send a POST request |
||
163 | * |
||
164 | * @param string $dataAsJson The data that will be sent to Socrata as JSON |
||
165 | * @param bool $associativeArray When true, the returned data will be associative arrays; otherwise, it'll be an |
||
166 | * StdClass object. |
||
167 | * @param array $headers An array where the return HTTP headers will be stored |
||
168 | * |
||
169 | * @see SodaClient::enableAssociativeArrays |
||
170 | * |
||
171 | * @since 0.1.0 |
||
172 | * |
||
173 | * @return mixed An associative array matching the returned JSON result or an StdClass object |
||
174 | */ |
||
175 | public function sendPost ($dataAsJson, $associativeArray, &$headers = NULL) |
||
186 | |||
187 | /** |
||
188 | * Send a PUT request |
||
189 | * |
||
190 | * @param string $dataAsJson The data that will be sent to Socrata as JSON |
||
191 | * @param bool $associativeArray When true, the returned data will be associative arrays; otherwise, it'll be an |
||
192 | * StdClass object. |
||
193 | * @param array $headers An array where the return HTTP headers will be stored |
||
194 | * |
||
195 | * @see SodaClient::enableAssociativeArrays |
||
196 | * |
||
197 | * @since 0.1.0 |
||
198 | * |
||
199 | * @return mixed An associative array matching the returned JSON result or an StdClass object |
||
200 | */ |
||
201 | public function sendPut ($dataAsJson, $associativeArray, &$headers = NULL) |
||
209 | |||
210 | /** |
||
211 | * Send a DELETE request |
||
212 | * |
||
213 | * @param bool $associativeArray When true, the returned data will be associative arrays; otherwise, it'll be an |
||
214 | * StdClass object. |
||
215 | * @param array $headers An array where the return HTTP headers will be stored |
||
216 | * |
||
217 | * @see SodaClient::enableAssociativeArrays |
||
218 | * |
||
219 | * @since 0.1.2 |
||
220 | * |
||
221 | * @return mixed An associative array matching the returned JSON result or an StdClass object |
||
222 | */ |
||
223 | public function sendDelete ($associativeArray, &$headers = NULL) |
||
229 | |||
230 | /** |
||
231 | * Set the POST fields that will be submitted in the cURL request |
||
232 | * |
||
233 | * @param string $dataAsJson The data that will be sent to Socrata as JSON |
||
234 | * |
||
235 | * @since 0.1.0 |
||
236 | */ |
||
237 | private function setPostFields ($dataAsJson) |
||
241 | |||
242 | /** |
||
243 | * Handle the execution of the cURL request. This function will also save the returned HTTP headers and handle them |
||
244 | * appropriately. |
||
245 | * |
||
246 | * @param bool $associativeArray When true, the returned data will be associative arrays; otherwise, it'll be an |
||
247 | * StdClass object. |
||
248 | * @param array $headers The reference to the array where the returned HTTP headers will be stored |
||
249 | * @param bool $ignoreReturn True if the returned body should be ignored |
||
250 | * |
||
251 | * @since 0.1.0 |
||
252 | * |
||
253 | * @throws \allejo\Socrata\Exceptions\CurlException If cURL is misconfigured or encounters an error |
||
254 | * @throws \allejo\Socrata\Exceptions\HttpException An HTTP status of something other 200 is returned |
||
255 | * @throws \allejo\Socrata\Exceptions\SodaException A SODA API error is returned |
||
256 | * |
||
257 | * @return mixed|NULL |
||
258 | */ |
||
259 | private function handleQuery ($associativeArray, &$headers, $ignoreReturn = false) |
||
284 | |||
285 | /** |
||
286 | * Configure the cURL instance and its credentials for Basic Authentication that this instance will be working with |
||
287 | * |
||
288 | * @param string $email The email for the user with Basic Authentication |
||
289 | * @param string $password The password for the user with Basic Authentication |
||
290 | * |
||
291 | * @since 0.1.0 |
||
292 | */ |
||
293 | private function configureCurl ($email, $password) |
||
310 | |||
311 | /** |
||
312 | * Execute the finalized cURL object that has already been configured |
||
313 | * |
||
314 | * @since 0.1.0 |
||
315 | * |
||
316 | * @throws \allejo\Socrata\Exceptions\CurlException If cURL is misconfigured or encounters an error |
||
317 | * |
||
318 | * @return mixed |
||
319 | */ |
||
320 | private function executeCurl () |
||
331 | |||
332 | /** |
||
333 | * Check for unexpected errors or SODA API errors |
||
334 | * |
||
335 | * @param string $body The body of the response |
||
336 | * @param string $result The unfiltered result cURL received |
||
337 | * |
||
338 | * @since 0.1.0 |
||
339 | * |
||
340 | * @throws \allejo\Socrata\Exceptions\HttpException If the $body returned was not a JSON object |
||
341 | * @throws \allejo\Socrata\Exceptions\SodaException The returned JSON object in the $body was a SODA API error |
||
342 | * |
||
343 | * @return mixed An associative array of the decoded JSON response |
||
344 | */ |
||
345 | private function handleResponseBody ($body, $result) |
||
365 | |||
366 | /** |
||
367 | * Handle the returned HTTP headers and save them into an array |
||
368 | * |
||
369 | * @param string $header The returned HTTP headers |
||
370 | * @param array $headers The reference to the array where our headers will be saved |
||
371 | * |
||
372 | * @since 0.1.0 |
||
373 | */ |
||
374 | private function saveHeaders ($header, &$headers) |
||
392 | |||
393 | /** |
||
394 | * Build a URL with GET parameters formatted into the URL |
||
395 | * |
||
396 | * @param string $url The base URL |
||
397 | * @param array $params The GET parameters that need to be appended to the base URL |
||
398 | * |
||
399 | * @since 0.1.0 |
||
400 | * |
||
401 | * @return string A URL with GET parameters |
||
402 | */ |
||
403 | private static function buildQuery ($url, $params = array()) |
||
414 | |||
415 | /** |
||
416 | * Format an array into a URL encoded values to be submitted in cURL requests |
||
417 | * |
||
418 | * **Input** |
||
419 | * |
||
420 | * ```php |
||
421 | * array( |
||
422 | * "foo" => "bar", |
||
423 | * "param" => "value" |
||
424 | * ) |
||
425 | * ``` |
||
426 | * |
||
427 | * **Output** |
||
428 | * |
||
429 | * ```php |
||
430 | * array( |
||
431 | * "foo=bar", |
||
432 | * "param=value" |
||
433 | * ) |
||
434 | * ``` |
||
435 | * |
||
436 | * @param array $params An array containing parameter names as keys and parameter values as values in the array. |
||
437 | * |
||
438 | * @return string[] A URL encoded and combined array of GET parameters to be sent |
||
439 | */ |
||
440 | private static function formatParameters ($params) |
||
451 | } |
||
452 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.