1 | <?php |
||
28 | class UrlQuery |
||
29 | { |
||
30 | /** |
||
31 | * Send a POST or PUT body as JSON instead of URL encoded values |
||
32 | */ |
||
33 | const BODY_AS_JSON = 0x1; |
||
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 | * Configure all of the authentication needed for cURL requests and the API endpoint |
||
51 | * |
||
52 | * @param string $url The API endpoint this instance will be calling |
||
53 | * @param array $urlParams Parameters that will be appended to the URL as GET parameters |
||
54 | * |
||
55 | * @since 0.1.0 |
||
56 | */ |
||
57 | 46 | public function __construct ($url, $urlParams) |
|
64 | |||
65 | /** |
||
66 | * Clean up after ourselves; clean up the cURL object. |
||
67 | */ |
||
68 | 46 | public function __destruct () |
|
72 | |||
73 | /** |
||
74 | * Set the credentials for basic authentication |
||
75 | * |
||
76 | * @param string $username The username basic authentication |
||
77 | * @param string $password The password basic authentication |
||
78 | * |
||
79 | * @since 0.1.0 |
||
80 | * |
||
81 | * @throws \InvalidArgumentException Either the username or the password was an empty or null string |
||
82 | */ |
||
83 | public function setAuthentication ($username, $password) |
||
84 | { |
||
85 | if (StringUtilities::isNullOrEmpty($username) || StringUtilities::isNullOrEmpty($password)) |
||
86 | { |
||
87 | throw new \InvalidArgumentException("Both the username and password must be non-empty strings."); |
||
88 | } |
||
89 | |||
90 | curl_setopt_array($this->cURL, array( |
||
91 | CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
||
92 | CURLOPT_USERPWD => $username . ":" . $password |
||
93 | )); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Set cURL headers |
||
98 | * |
||
99 | * @param array $headers The headers that will be sent with cURL |
||
100 | * |
||
101 | * @since 0.1.0 |
||
102 | * |
||
103 | * @throws \InvalidArgumentException The $headers parameter was not an array or it was an empty array |
||
104 | */ |
||
105 | public function setHeaders ($headers) |
||
106 | { |
||
107 | if (empty($headers) || !is_array($headers)) |
||
108 | { |
||
109 | throw new \InvalidArgumentException("The headers parameter must be a non-empty array"); |
||
110 | } |
||
111 | |||
112 | curl_setopt_array($this->cURL, array( |
||
113 | CURLOPT_HEADER => true, |
||
114 | CURLOPT_HTTPHEADER => $headers |
||
115 | )); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Send a GET request |
||
120 | * |
||
121 | * @since 0.1.0 |
||
122 | * |
||
123 | * @return mixed An associative array matching the returned JSON result |
||
124 | */ |
||
125 | 46 | public function sendGet () |
|
129 | |||
130 | /** |
||
131 | * Send a POST request |
||
132 | * |
||
133 | * @param array $postArray The data that will be sent to DaPulse |
||
134 | * @param null|int $flags Available flags: BODY_AS_JSON |
||
135 | * |
||
136 | * @since 0.1.0 |
||
137 | * |
||
138 | * @throws HttpException |
||
139 | * |
||
140 | * @return mixed An associative array matching the returned JSON result |
||
141 | */ |
||
142 | public function sendPost ($postArray, $flags = null) |
||
143 | { |
||
144 | $this->setPostFields($postArray, $flags); |
||
|
|||
145 | |||
146 | curl_setopt_array($this->cURL, array( |
||
147 | CURLOPT_POST => true, |
||
148 | CURLOPT_CUSTOMREQUEST => "POST" |
||
149 | )); |
||
150 | |||
151 | return $this->handleQuery(); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Send a PUT request |
||
156 | * |
||
157 | * @param array $postArray The data that will be sent to DaPulse |
||
158 | * @param null|int $flags Available flags: BODY_AS_JSON |
||
159 | * |
||
160 | * @since 0.1.0 |
||
161 | * |
||
162 | * @return mixed An associative array matching the returned JSON result |
||
163 | */ |
||
164 | 7 | public function sendPut ($postArray, $flags = null) |
|
172 | |||
173 | /** |
||
174 | * Send a DELETE request |
||
175 | * |
||
176 | * @since 0.1.0 |
||
177 | * |
||
178 | * @return mixed An associative array matching the returned JSON result |
||
179 | */ |
||
180 | public function sendDelete () |
||
186 | |||
187 | /** |
||
188 | * Set the POST fields that will be submitted in the cURL request |
||
189 | * |
||
190 | * @param string $postArray The POST fields that will be sent to DaPulse |
||
191 | * @param null|int $flags Available flags: BODY_AS_JSON |
||
192 | * |
||
193 | * @since 0.1.0 |
||
194 | */ |
||
195 | 7 | private function setPostFields ($postArray, $flags) |
|
208 | |||
209 | /** |
||
210 | * Handle the execution of the cURL request. This function will also save the returned HTTP headers and handle them |
||
211 | * appropriately. |
||
212 | * |
||
213 | * @since 0.1.0 |
||
214 | * |
||
215 | * @throws CurlException If cURL is misconfigured or encounters an error |
||
216 | * @throws HttpException An HTTP status of something other 200 is returned |
||
217 | * |
||
218 | * @return mixed |
||
219 | */ |
||
220 | 46 | private function handleQuery () |
|
232 | |||
233 | /** |
||
234 | * Configure the cURL instance and its credentials for Basic Authentication that this instance will be working with |
||
235 | * |
||
236 | * @since 0.1.0 |
||
237 | */ |
||
238 | 46 | private function configureCurl () |
|
245 | |||
246 | /** |
||
247 | * Execute the finalized cURL object that has already been configured |
||
248 | * |
||
249 | * @since 0.1.0 |
||
250 | * |
||
251 | * @throws \allejo\DaPulse\Exceptions\CurlException If cURL is misconfigured or encounters an error |
||
252 | * |
||
253 | * @return mixed |
||
254 | */ |
||
255 | 46 | private function executeCurl () |
|
266 | |||
267 | /** |
||
268 | * Format an array into a URL encoded values to be submitted in cURL requests |
||
269 | * |
||
270 | * **Input** |
||
271 | * |
||
272 | * ```php |
||
273 | * array( |
||
274 | * "foo" => "bar", |
||
275 | * "param" => "value" |
||
276 | * ) |
||
277 | * ``` |
||
278 | * |
||
279 | * **Output** |
||
280 | * |
||
281 | * ```php |
||
282 | * array( |
||
283 | * "foo=bar", |
||
284 | * "param=value" |
||
285 | * ) |
||
286 | * ``` |
||
287 | * |
||
288 | * @param array $params An array containing parameter names as keys and parameter values as values in the array. |
||
289 | * |
||
290 | * @since 0.1.0 |
||
291 | * |
||
292 | * @return string A URL encoded and combined array of GET or POST parameters to be sent |
||
293 | */ |
||
294 | 46 | private static function formatParameters ($params) |
|
321 | |||
322 | /** |
||
323 | * Convert an indexed array into an array that can be feed to `formatParameters()` to be formatted to an acceptable |
||
324 | * structure to be sent via a GET or POST request. |
||
325 | * |
||
326 | * **Input** |
||
327 | * |
||
328 | * ```php |
||
329 | * array( |
||
330 | * "first", |
||
331 | * "second", |
||
332 | * "third" |
||
333 | * ) |
||
334 | * ``` |
||
335 | * |
||
336 | * **Output** |
||
337 | * |
||
338 | * ```php |
||
339 | * array( |
||
340 | * "prefix[0]" => "first", |
||
341 | * "prefix[1]" => "second", |
||
342 | * "prefix[2]" => "third", |
||
343 | * ) |
||
344 | * ``` |
||
345 | * |
||
346 | * @param string $prefix The name of the |
||
347 | * @param string[] $array |
||
348 | * |
||
349 | * @see formatParameters() |
||
350 | * |
||
351 | * @since 0.1.0 |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | private static function formatArray ($prefix, $array) |
||
367 | } |
||
368 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: