1 | <?php |
||||
2 | /** |
||||
3 | * This file is part of the O2System Framework package. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Steeve Andrian Salim |
||||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
10 | */ |
||||
11 | |||||
12 | // ------------------------------------------------------------------------ |
||||
13 | |||||
14 | namespace O2System\Curl; |
||||
15 | |||||
16 | // ------------------------------------------------------------------------ |
||||
17 | |||||
18 | use O2System\Kernel\Http\Message\Uri; |
||||
19 | use O2System\Psr\Http\Message\UriInterface; |
||||
20 | use O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException; |
||||
21 | |||||
22 | /** |
||||
23 | * Class Request |
||||
24 | * |
||||
25 | * @package O2System\Curl |
||||
26 | */ |
||||
27 | class Request |
||||
28 | { |
||||
29 | /** |
||||
30 | * Request::$curlAutoClose |
||||
31 | * |
||||
32 | * Flag for automatic close the connection when it has finished processing |
||||
33 | * and not be pooled for reuse. |
||||
34 | * |
||||
35 | * @var bool |
||||
36 | */ |
||||
37 | public $curlAutoClose = true; |
||||
38 | /** |
||||
39 | * Request::$uri |
||||
40 | * |
||||
41 | * Request uri instance. |
||||
42 | * |
||||
43 | * @var Uri |
||||
44 | */ |
||||
45 | protected $uri; |
||||
46 | |||||
47 | /** |
||||
48 | * Request::$curlOptions |
||||
49 | * |
||||
50 | * Request Curl handle options. |
||||
51 | * |
||||
52 | * @var array |
||||
53 | */ |
||||
54 | protected $curlOptions; |
||||
55 | /** |
||||
56 | * Request::$curlHeaders |
||||
57 | * |
||||
58 | * Request Curl handle headers. |
||||
59 | * |
||||
60 | * @var array |
||||
61 | */ |
||||
62 | protected $curlHeaders = []; |
||||
63 | |||||
64 | // ------------------------------------------------------------------------ |
||||
65 | |||||
66 | /** |
||||
67 | * Request::__construct |
||||
68 | * |
||||
69 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadPhpExtensionCallException |
||||
70 | */ |
||||
71 | public function __construct() |
||||
72 | { |
||||
73 | if ( ! function_exists('curl_init')) { |
||||
74 | throw new BadPhpExtensionCallException('E_CURL_NOT_LOADED'); |
||||
75 | } |
||||
76 | |||||
77 | // default, TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. |
||||
78 | $this->curlOptions[ CURLOPT_RETURNTRANSFER ] = true; |
||||
79 | |||||
80 | // default, TRUE to output verbose information. |
||||
81 | $this->curlOptions[ CURLOPT_VERBOSE ] = true; |
||||
82 | |||||
83 | // default, TRUE to include the header in the output. |
||||
84 | $this->curlOptions[ CURLOPT_HEADER ] = true; |
||||
85 | |||||
86 | // default, lets CURL decide which version to use. |
||||
87 | $this->curlOptions[ CURLOPT_HTTP_VERSION ] = CURL_HTTP_VERSION_NONE; |
||||
88 | |||||
89 | // default, http user agent using o2system curl. |
||||
90 | $this->curlOptions[ CURLOPT_USERAGENT ] = 'Curl/1.0 (O2System)'; |
||||
91 | |||||
92 | // default, TRUE to automatically set the Referer: field in requests where it follows a Location: redirect. |
||||
93 | $this->curlOptions[ CURLOPT_AUTOREFERER ] = true; |
||||
94 | |||||
95 | // default, FALSE to stop cURL from verifying the peer's certificate. |
||||
96 | $this->curlOptions[ CURLOPT_SSL_VERIFYPEER ] = false; |
||||
97 | |||||
98 | // default, 0 to not check the names. |
||||
99 | $this->curlOptions[ CURLOPT_SSL_VERIFYHOST ] = 0; |
||||
100 | } |
||||
101 | |||||
102 | // ------------------------------------------------------------------------ |
||||
103 | |||||
104 | /** |
||||
105 | * Request::setOptions |
||||
106 | * |
||||
107 | * Sets curl options. |
||||
108 | * |
||||
109 | * @see http://php.net/manual/en/function.curl-setopt.php |
||||
110 | * |
||||
111 | * @param array $options |
||||
112 | * |
||||
113 | * @return static |
||||
114 | */ |
||||
115 | public function setOptions(array $options) |
||||
116 | { |
||||
117 | foreach ($options as $option => $value) { |
||||
118 | $this->setOption($option, $value); |
||||
119 | } |
||||
120 | |||||
121 | return $this; |
||||
122 | } |
||||
123 | |||||
124 | // ------------------------------------------------------------------------ |
||||
125 | |||||
126 | /** |
||||
127 | * Request::setOption |
||||
128 | * |
||||
129 | * Sets custom curl option. |
||||
130 | * |
||||
131 | * @see http://php.net/manual/en/function.curl-setopt.php |
||||
132 | * |
||||
133 | * @param int $option The curl option number. |
||||
134 | * @param mixed $value The value of curl option. |
||||
135 | * |
||||
136 | * @return static |
||||
137 | */ |
||||
138 | public function setOption($option, $value) |
||||
139 | { |
||||
140 | $this->curlOptions[ $option ] = $value; |
||||
141 | |||||
142 | return $this; |
||||
143 | } |
||||
144 | |||||
145 | // ------------------------------------------------------------------------ |
||||
146 | |||||
147 | /** |
||||
148 | * Request::setUri |
||||
149 | * |
||||
150 | * @param \O2System\Psr\Http\Message\UriInterface $uri |
||||
151 | * |
||||
152 | * @return static |
||||
153 | */ |
||||
154 | public function setUri(UriInterface $uri) |
||||
155 | { |
||||
156 | $this->uri = $uri; |
||||
0 ignored issues
–
show
|
|||||
157 | |||||
158 | return $this; |
||||
159 | } |
||||
160 | |||||
161 | // ------------------------------------------------------------------------ |
||||
162 | |||||
163 | /** |
||||
164 | * Request::setHttpVersion |
||||
165 | * |
||||
166 | * Sets which http version to use (default, lets CURL decide which version to use). |
||||
167 | * |
||||
168 | * @param int $httpVersion Supported encodings are "CURL_HTTP_VERSION_NONE", "CURL_HTTP_VERSION_1_0", |
||||
169 | * "CURL_HTTP_VERSION_1_1", and "CURL_HTTP_VERSION_2". |
||||
170 | * |
||||
171 | * @return static |
||||
172 | */ |
||||
173 | public function setHttpVersion($httpVersion) |
||||
174 | { |
||||
175 | if (in_array($httpVersion, [ |
||||
176 | CURL_HTTP_VERSION_NONE, |
||||
177 | CURL_HTTP_VERSION_1_0, |
||||
178 | CURL_HTTP_VERSION_1_1, |
||||
179 | CURL_HTTP_VERSION_2, |
||||
180 | ] |
||||
181 | )) { |
||||
182 | $this->curlOptions[ CURLOPT_HTTP_VERSION ] = $httpVersion; |
||||
183 | } |
||||
184 | |||||
185 | return $this; |
||||
186 | } |
||||
187 | |||||
188 | // ------------------------------------------------------------------------ |
||||
189 | |||||
190 | /** |
||||
191 | * Request::setUserAgent |
||||
192 | * |
||||
193 | * Sets the contents of the "User-Agent: " header to be used in a HTTP request. |
||||
194 | * |
||||
195 | * @param string $userAgent |
||||
196 | * |
||||
197 | * @return static |
||||
198 | */ |
||||
199 | public function setUserAgent($userAgent) |
||||
200 | { |
||||
201 | $this->curlOptions[ CURLOPT_USERAGENT ] = trim($userAgent); |
||||
202 | |||||
203 | return $this; |
||||
204 | } |
||||
205 | |||||
206 | // ------------------------------------------------------------------------ |
||||
207 | |||||
208 | /** |
||||
209 | * Request::setReferer |
||||
210 | * |
||||
211 | * Sets the contents of the "Referer: " header to be used in a HTTP request. |
||||
212 | * |
||||
213 | * @param string $referer |
||||
214 | * |
||||
215 | * @return static |
||||
216 | */ |
||||
217 | public function setReferer($referer) |
||||
218 | { |
||||
219 | $this->curlOptions[ CURLOPT_REFERER ] = trim($referer); |
||||
220 | |||||
221 | return $this; |
||||
222 | } |
||||
223 | |||||
224 | // ------------------------------------------------------------------------ |
||||
225 | |||||
226 | /** |
||||
227 | * Request::setEncoding |
||||
228 | * |
||||
229 | * Sets the contents of the "Accept-Encoding: " header. This enables decoding of the response. |
||||
230 | * Supported encodings are "identity", "deflate", and "gzip". |
||||
231 | * If an empty string, "", is set, a header containing all supported encoding types is sent. |
||||
232 | * |
||||
233 | * @param string $encoding Supported encodings are "identity", "deflate", and "gzip". |
||||
234 | * |
||||
235 | * @return static |
||||
236 | */ |
||||
237 | public function setEncoding($encoding) |
||||
238 | { |
||||
239 | if (in_array($encoding, ['identity', 'deflate', 'gzip'])) { |
||||
240 | $this->curlOptions[ CURLOPT_ENCODING ] = $encoding; |
||||
241 | } else { |
||||
242 | $this->curlOptions[ CURLOPT_ENCODING ] = ''; |
||||
243 | } |
||||
244 | |||||
245 | return $this; |
||||
246 | } |
||||
247 | |||||
248 | // ------------------------------------------------------------------------ |
||||
249 | |||||
250 | /** |
||||
251 | * Request::setTimeout |
||||
252 | * |
||||
253 | * Sets the maximum number of seconds to allow cURL functions to execute. |
||||
254 | * |
||||
255 | * @param int $timeout The number of seconds. |
||||
256 | * @param bool $isMilliseconds The number units is uses milliseconds format. |
||||
257 | * |
||||
258 | * @return static |
||||
259 | */ |
||||
260 | public function setTimeout($timeout, $isMilliseconds = false) |
||||
261 | { |
||||
262 | if ($isMilliseconds) { |
||||
263 | $this->curlOptions[ CURLOPT_TIMEOUT_MS ] = (int)$timeout; |
||||
264 | } else { |
||||
265 | $this->curlOptions[ CURLOPT_TIMEOUT ] = (int)$timeout; |
||||
266 | } |
||||
267 | |||||
268 | return $this; |
||||
269 | } |
||||
270 | |||||
271 | // ------------------------------------------------------------------------ |
||||
272 | |||||
273 | /** |
||||
274 | * Request::setConnectionTimeout |
||||
275 | * |
||||
276 | * Sets the number of seconds to wait while trying to connect. Use 0 to wait indefinitely. |
||||
277 | * |
||||
278 | * @param int $timeout The number of seconds. |
||||
279 | * @param bool $isMilliseconds The number units is uses milliseconds format. |
||||
280 | * |
||||
281 | * @return static |
||||
282 | */ |
||||
283 | public function setConnectionTimeout($timeout, $isMilliseconds = false) |
||||
284 | { |
||||
285 | if ($isMilliseconds) { |
||||
286 | $this->curlOptions[ CURLOPT_CONNECTTIMEOUT_MS ] = (int)$timeout; |
||||
287 | } else { |
||||
288 | $this->curlOptions[ CURLOPT_CONNECTTIMEOUT ] = (int)$timeout; |
||||
289 | } |
||||
290 | |||||
291 | return $this; |
||||
292 | } |
||||
293 | |||||
294 | // ------------------------------------------------------------------------ |
||||
295 | |||||
296 | /** |
||||
297 | * Request::setMaximumRedirects |
||||
298 | * |
||||
299 | * Sets the maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION. |
||||
300 | * |
||||
301 | * @param string $maximum The numbers of maximum redirections. |
||||
302 | * |
||||
303 | * @return static |
||||
304 | */ |
||||
305 | public function setMaximumRedirects($maximum) |
||||
306 | { |
||||
307 | $this->curlOptions[ CURLOPT_MAXREDIRS ] = (int)$maximum; |
||||
308 | $this->curlOptions[ CURLOPT_FOLLOWLOCATION ] = true; |
||||
309 | |||||
310 | return $this; |
||||
311 | } |
||||
312 | |||||
313 | // ------------------------------------------------------------------------ |
||||
314 | |||||
315 | /** |
||||
316 | * Request::setSslCaInfo |
||||
317 | * |
||||
318 | * Sets the name of a file holding one or more certificates to verify the peer with. |
||||
319 | * It's also set the curl options: |
||||
320 | * 1. CURLOPT_SSL_VERIFYPEER value into TRUE. |
||||
321 | * 2. CURLOPT_SSL_VERIFYHOST value into 2 to check the existence of a common name and also verify that it matches |
||||
322 | * the hostname provided. |
||||
323 | * 3. CURLOPT_SSL_VERIFYSTATUS value into TRUE to verify the certificate status. |
||||
324 | * |
||||
325 | * @param string $caInfoFilePath Path to ssl certificate file. |
||||
326 | * |
||||
327 | * @return static |
||||
328 | */ |
||||
329 | public function setSslCaInfo($caInfoFilePath) |
||||
330 | { |
||||
331 | if (is_file($caInfoFilePath)) { |
||||
332 | $this->setSslVerify(2, true); |
||||
333 | $this->curlOptions[ CURLOPT_CAINFO ] = pathinfo($caInfoFilePath, PATHINFO_BASENAME); |
||||
334 | $this->curlOptions[ CURLOPT_CAPATH ] = dirname($caInfoFilePath); |
||||
335 | } |
||||
336 | |||||
337 | return $this; |
||||
338 | } |
||||
339 | |||||
340 | // ------------------------------------------------------------------------ |
||||
341 | |||||
342 | /** |
||||
343 | * Request::setSslVerify |
||||
344 | * |
||||
345 | * Sets the SSL Verify Peer into TRUE. |
||||
346 | * |
||||
347 | * @param int $verifyHost 0. To not check the names. In production environments the value of this option should |
||||
348 | * be kept at 2 (default value). |
||||
349 | * 1. To check the existence of a common name in the SSL peer certificate. |
||||
350 | * 2. To check the existence of a common name and also verify that it matches the |
||||
351 | * hostname provided. |
||||
352 | * @param bool $verifyStatus TRUE to verify the certificate's status. |
||||
353 | * |
||||
354 | * @return static |
||||
355 | */ |
||||
356 | public function setSslVerify($verifyHost, $verifyStatus = false) |
||||
357 | { |
||||
358 | $this->curlOptions[ CURLOPT_SSL_VERIFYPEER ] = true; |
||||
359 | |||||
360 | $verifyHost = in_array($verifyHost, range(0, 3)) ? $verifyHost : 0; |
||||
361 | $this->curlOptions[ CURLOPT_SSL_VERIFYHOST ] = (int)$verifyHost; |
||||
362 | $this->curlOptions[ CURLOPT_SSL_VERIFYSTATUS ] = (bool)$verifyStatus; |
||||
363 | |||||
364 | return $this; |
||||
365 | } |
||||
366 | |||||
367 | // ------------------------------------------------------------------------ |
||||
368 | |||||
369 | /** |
||||
370 | * Request::setAuthentication |
||||
371 | * |
||||
372 | * Sets the HTTP authentication method(s) to use. |
||||
373 | * |
||||
374 | * @param string $username The HTTP authentication username. |
||||
375 | * @param string $password The HTTP authentication password. |
||||
376 | * @param int|string $method The HTTP authentication method. The options are: |
||||
377 | * 1. CURLAUTH_BASIC |
||||
378 | * 2. CURLAUTH_DIGEST |
||||
379 | * 3. CURLAUTH_GSSNEGOTIATE |
||||
380 | * 4. CURLAUTH_NTLM |
||||
381 | * 5. CURLAUTH_ANY (default) |
||||
382 | * 6. CURLAUTH_ANYSAFE |
||||
383 | * |
||||
384 | * @return static |
||||
385 | */ |
||||
386 | public function setAuthentication($username = '', $password = '', $method = CURLAUTH_ANY) |
||||
387 | { |
||||
388 | if (defined('CURLOPT_USERNAME')) { |
||||
389 | $this->curlOptions[ CURLOPT_USERNAME ] = $username; |
||||
390 | } |
||||
391 | |||||
392 | $this->curlOptions[ CURLOPT_USERPWD ] = "$username:$password"; |
||||
393 | $this->curlOptions[ CURLOPT_HTTPAUTH ] = CURLAUTH_ANY; |
||||
394 | |||||
395 | if (in_array($method, [ |
||||
396 | CURLAUTH_BASIC, |
||||
397 | CURLAUTH_DIGEST, |
||||
398 | CURLAUTH_GSSNEGOTIATE, |
||||
399 | CURLAUTH_NTLM, |
||||
400 | CURLAUTH_ANY, |
||||
401 | CURLAUTH_ANYSAFE, |
||||
402 | ])) { |
||||
403 | $this->curlOptions[ CURLOPT_HTTPAUTH ] = $method; |
||||
404 | } |
||||
405 | |||||
406 | return $this; |
||||
407 | } |
||||
408 | |||||
409 | // ------------------------------------------------------------------------ |
||||
410 | |||||
411 | /** |
||||
412 | * Request::setProxy |
||||
413 | * |
||||
414 | * Set the HTTP proxy to tunnel requests through. |
||||
415 | * |
||||
416 | * @param string $address The HTTP proxy address. |
||||
417 | * @param int|string $port The HTTP proxy port. |
||||
418 | * @param int|string $type The HTTP proxy type, available options: |
||||
419 | * 1. CURLPROXY_HTTP |
||||
420 | * 2. CURLPROXY_HTTP_1_0 |
||||
421 | * 3. CURLPROXY_SOCKS4 |
||||
422 | * 4. CURLPROXY_SOCKS5 |
||||
423 | * 5. CURLPROXY_SOCKS4A |
||||
424 | * 6. CURLPROXY_SOCKS5_HOSTNAME |
||||
425 | * |
||||
426 | * @return static |
||||
427 | */ |
||||
428 | public function setProxy($address, $port = 1080, $type = CURLPROXY_HTTP) |
||||
429 | { |
||||
430 | $this->curlOptions[ CURLOPT_PROXY ] = $address; |
||||
431 | $this->curlOptions[ CURLOPT_PROXYPORT ] = $port; |
||||
432 | $this->curlOptions[ CURLOPT_PROXYTYPE ] = $type; |
||||
433 | $this->curlOptions[ CURLOPT_HTTPPROXYTUNNEL ] = true; |
||||
434 | |||||
435 | return $this; |
||||
436 | } |
||||
437 | |||||
438 | // ------------------------------------------------------------------------ |
||||
439 | |||||
440 | /** |
||||
441 | * Request::setProxyAuthentication |
||||
442 | * |
||||
443 | * Sets the username and password to use for the connection to the proxy. |
||||
444 | * |
||||
445 | * @param string $username The HTTP Proxy authentication username. |
||||
446 | * @param string $password The HTTP Proxy authentication password. |
||||
447 | * @param int|string $method The HTTP Proxy authentication method. The options are: |
||||
448 | * 1. CURLAUTH_BASIC |
||||
449 | * 2. CURLAUTH_NTLM |
||||
450 | * |
||||
451 | * @return static |
||||
452 | */ |
||||
453 | public function setProxyAuthentication($username, $password, $method = CURLAUTH_BASIC) |
||||
454 | { |
||||
455 | if (array_key_exists(CURLOPT_HTTPPROXYTUNNEL, $this->curlOptions)) { |
||||
456 | $this->curlOptions[ CURLOPT_PROXYUSERPWD ] = "$username:$password"; |
||||
457 | } |
||||
458 | |||||
459 | $this->curlOptions[ CURLOPT_PROXYAUTH ] = CURLAUTH_BASIC; |
||||
460 | |||||
461 | if (in_array($method, [ |
||||
462 | CURLAUTH_BASIC, |
||||
463 | CURLAUTH_NTLM, |
||||
464 | ])) { |
||||
465 | $this->curlOptions[ CURLOPT_PROXYAUTH ] = $method; |
||||
466 | } |
||||
467 | |||||
468 | return $this; |
||||
469 | } |
||||
470 | |||||
471 | // ------------------------------------------------------------------------ |
||||
472 | |||||
473 | /** |
||||
474 | * Request::setHeaders |
||||
475 | * |
||||
476 | * Sets curl request with headers. |
||||
477 | * |
||||
478 | * @param array $headers |
||||
479 | * |
||||
480 | * @return static |
||||
481 | */ |
||||
482 | public function setHeaders(array $headers) |
||||
483 | { |
||||
484 | foreach ($headers as $name => $value) { |
||||
485 | $this->addHeader($name, $value); |
||||
486 | } |
||||
487 | |||||
488 | return $this; |
||||
489 | } |
||||
490 | |||||
491 | // ------------------------------------------------------------------------ |
||||
492 | |||||
493 | /** |
||||
494 | * Request::addHeader |
||||
495 | * |
||||
496 | * Add curl request header. |
||||
497 | * |
||||
498 | * @param string $name Case-insensitive header field name. |
||||
499 | * @param string|string[] $value Header value(s). |
||||
500 | * |
||||
501 | * @return static |
||||
502 | */ |
||||
503 | public function addHeader($name, $value) |
||||
504 | { |
||||
505 | $this->curlHeaders[ $name ] = $value; |
||||
506 | |||||
507 | return $this; |
||||
508 | } |
||||
509 | |||||
510 | // ------------------------------------------------------------------------ |
||||
511 | |||||
512 | /** |
||||
513 | * Request::setCookie |
||||
514 | * |
||||
515 | * Sets the cookie contents to be used in the HTTP request. |
||||
516 | * |
||||
517 | * @param string $cookieFile The contents of the "Cookie: " header to be used in the HTTP request. |
||||
518 | * Note that multiple cookies are separated with a semicolon followed by a space |
||||
519 | * (e.g., "fruit=apple; colour=red") |
||||
520 | * |
||||
521 | * @return static |
||||
522 | */ |
||||
523 | public function setCookie($cookie) |
||||
524 | { |
||||
525 | $this->curlOptions[ CURLOPT_COOKIE ] = $cookie; |
||||
526 | |||||
527 | return $this; |
||||
528 | } |
||||
529 | |||||
530 | // ------------------------------------------------------------------------ |
||||
531 | |||||
532 | /** |
||||
533 | * Request::setCookieFile |
||||
534 | * |
||||
535 | * Sets the cookie file to be used in the HTTP request. |
||||
536 | * |
||||
537 | * @param string $cookieFile The name of the file containing the cookie data. |
||||
538 | * @param string|null $cookieJar The name of a file to save all internal cookies to when the handle is closed, |
||||
539 | * e.g. after a call to curl_close. |
||||
540 | * |
||||
541 | * @return static |
||||
542 | */ |
||||
543 | public function setCookieFile($cookieFile, $cookieJar = null) |
||||
544 | { |
||||
545 | if (is_file($cookieFile)) { |
||||
546 | $cookieJar = empty($cookieJar) ? $cookieFile : $cookieJar; |
||||
547 | $this->curlOptions[ CURLOPT_COOKIEFILE ] = $cookieFile; |
||||
548 | $this->curlOptions[ CURLOPT_COOKIEJAR ] = $cookieJar; |
||||
549 | } |
||||
550 | |||||
551 | return $this; |
||||
552 | } |
||||
553 | |||||
554 | // ------------------------------------------------------------------------ |
||||
555 | |||||
556 | /** |
||||
557 | * Request::get |
||||
558 | * |
||||
559 | * Get response use HTTP GET request method. |
||||
560 | * |
||||
561 | * @param array $query Additional HTTP GET query. |
||||
562 | * |
||||
563 | * @return bool|\O2System\Curl\Response |
||||
564 | */ |
||||
565 | public function get(array $query = []) |
||||
566 | { |
||||
567 | $this->uri = $this->uri->withQuery($query); |
||||
568 | |||||
569 | $this->curlOptions[ CURLOPT_HTTPGET ] = true; |
||||
570 | |||||
571 | return $this->getResponse(); |
||||
572 | } |
||||
573 | |||||
574 | // ------------------------------------------------------------------------ |
||||
575 | |||||
576 | /** |
||||
577 | * Request::getResponse |
||||
578 | * |
||||
579 | * Get curl response. |
||||
580 | * |
||||
581 | * @return Response|bool |
||||
582 | */ |
||||
583 | public function getResponse() |
||||
584 | { |
||||
585 | if (array_key_exists(CURLOPT_URL, $this->curlOptions)) { |
||||
586 | $handle = curl_init(); |
||||
587 | } else { |
||||
588 | $handle = curl_init($this->uri->__toString()); |
||||
589 | } |
||||
590 | |||||
591 | $headers = []; |
||||
592 | if (count($this->curlHeaders)) { |
||||
593 | foreach ($this->curlHeaders as $key => $value) { |
||||
594 | $headers[] = trim($key) . ': ' . trim($value); |
||||
595 | } |
||||
596 | |||||
597 | $this->curlOptions[ CURLOPT_HTTPHEADER ] = $headers; |
||||
598 | } |
||||
599 | |||||
600 | if (curl_setopt_array($handle, $this->curlOptions)) { |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_setopt_array() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
601 | $response = (new Response($handle)) |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $curlHandle of O2System\Curl\Response::__construct() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
602 | ->setInfo(curl_getinfo($handle)) |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_getinfo() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
603 | ->setContent(curl_exec($handle)); |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
604 | |||||
605 | if ($this->curlAutoClose) { |
||||
606 | curl_close($handle); |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_close() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
607 | } |
||||
608 | |||||
609 | return $response; |
||||
610 | } |
||||
611 | |||||
612 | return false; |
||||
613 | } |
||||
614 | |||||
615 | // ------------------------------------------------------------------------ |
||||
616 | |||||
617 | /** |
||||
618 | * Request::post |
||||
619 | * |
||||
620 | * Get response use HTTP POST request method. |
||||
621 | * |
||||
622 | * @param array $fields Additional HTTP POST fields. |
||||
623 | * |
||||
624 | * @return bool|\O2System\Curl\Response |
||||
625 | */ |
||||
626 | public function post(array $fields = [], $json = false) |
||||
627 | { |
||||
628 | $this->curlOptions[ CURLOPT_POST ] = true; |
||||
629 | |||||
630 | if ($json === true) { |
||||
631 | $this->curlOptions[ CURLOPT_POSTFIELDS ] = json_encode($fields); |
||||
632 | $this->addHeader('Content-Type', 'application/json'); |
||||
633 | $this->addHeader('Content-Length', strlen($this->curlOptions[ CURLOPT_POSTFIELDS ])); |
||||
634 | } else { |
||||
635 | $this->curlOptions[ CURLOPT_POSTFIELDS ] = http_build_query($fields, null, '&', PHP_QUERY_RFC3986); |
||||
636 | } |
||||
637 | |||||
638 | return $this->getResponse(); |
||||
639 | } |
||||
640 | |||||
641 | // ------------------------------------------------------------------------ |
||||
642 | |||||
643 | /** |
||||
644 | * Request::delete |
||||
645 | * |
||||
646 | * Get response use custom HTTP DELETE request method. |
||||
647 | * |
||||
648 | * @param array $fields Additional HTTP POST fields. |
||||
649 | * |
||||
650 | * @return bool|\O2System\Curl\Response |
||||
651 | */ |
||||
652 | public function delete(array $fields = []) |
||||
653 | { |
||||
654 | $this->curlOptions[ CURLOPT_CUSTOMREQUEST ] = 'DELETE'; |
||||
655 | |||||
656 | if (count($fields)) { |
||||
657 | $this->curlOptions[ CURLOPT_POST ] = true; |
||||
658 | $this->curlOptions[ CURLOPT_POSTFIELDS ] = http_build_query($fields, null, '&', PHP_QUERY_RFC3986); |
||||
659 | } |
||||
660 | |||||
661 | return $this->getResponse(); |
||||
662 | } |
||||
663 | |||||
664 | // ------------------------------------------------------------------------ |
||||
665 | |||||
666 | /** |
||||
667 | * Request::head |
||||
668 | * |
||||
669 | * Get response use custom HTTP HEAD request method. |
||||
670 | * |
||||
671 | * @return bool|\O2System\Curl\Response |
||||
672 | */ |
||||
673 | public function head() |
||||
674 | { |
||||
675 | $this->curlOptions[ CURLOPT_CUSTOMREQUEST ] = 'HEAD'; |
||||
676 | $this->curlOptions[ CURLOPT_HTTPGET ] = true; |
||||
677 | $this->curlOptions[ CURLOPT_HEADER ] = true; |
||||
678 | $this->curlOptions[ CURLOPT_NOBODY ] = true; |
||||
679 | |||||
680 | return $this->getResponse(); |
||||
681 | } |
||||
682 | |||||
683 | // ------------------------------------------------------------------------ |
||||
684 | |||||
685 | /** |
||||
686 | * Request::trace |
||||
687 | * |
||||
688 | * Get response use custom HTTP TRACE request method. |
||||
689 | * |
||||
690 | * @return bool|\O2System\Curl\Response |
||||
691 | */ |
||||
692 | public function trace() |
||||
693 | { |
||||
694 | $this->curlOptions[ CURLOPT_CUSTOMREQUEST ] = 'TRACE'; |
||||
695 | |||||
696 | return $this->getResponse(); |
||||
697 | } |
||||
698 | |||||
699 | // ------------------------------------------------------------------------ |
||||
700 | |||||
701 | /** |
||||
702 | * Request::trace |
||||
703 | * |
||||
704 | * Get response use custom HTTP OPTIONS request method. |
||||
705 | * |
||||
706 | * @return bool|\O2System\Curl\Response |
||||
707 | */ |
||||
708 | public function options() |
||||
709 | { |
||||
710 | $this->curlOptions[ CURLOPT_CUSTOMREQUEST ] = 'OPTIONS'; |
||||
711 | |||||
712 | return $this->getResponse(); |
||||
713 | } |
||||
714 | |||||
715 | // ------------------------------------------------------------------------ |
||||
716 | |||||
717 | /** |
||||
718 | * Request::patch |
||||
719 | * |
||||
720 | * Get response use custom HTTP PATCH request method. |
||||
721 | * |
||||
722 | * @return bool|\O2System\Curl\Response |
||||
723 | */ |
||||
724 | public function patch() |
||||
725 | { |
||||
726 | $this->curlOptions[ CURLOPT_CUSTOMREQUEST ] = 'PATCH'; |
||||
727 | |||||
728 | return $this->getResponse(); |
||||
729 | } |
||||
730 | |||||
731 | // ------------------------------------------------------------------------ |
||||
732 | |||||
733 | /** |
||||
734 | * Request::connect |
||||
735 | * |
||||
736 | * Get response use custom HTTP CONNECT request method. |
||||
737 | * |
||||
738 | * @return bool|\O2System\Curl\Response |
||||
739 | */ |
||||
740 | public function connect() |
||||
741 | { |
||||
742 | $this->curlOptions[ CURLOPT_CUSTOMREQUEST ] = 'CONNECT'; |
||||
743 | |||||
744 | return $this->getResponse(); |
||||
745 | } |
||||
746 | |||||
747 | // ------------------------------------------------------------------------ |
||||
748 | |||||
749 | /** |
||||
750 | * Request::download |
||||
751 | * |
||||
752 | * Get response use custom HTTP DOWNLOAD request method. |
||||
753 | * |
||||
754 | * @return bool|\O2System\Curl\Response |
||||
755 | */ |
||||
756 | public function download() |
||||
757 | { |
||||
758 | $this->curlOptions[ CURLOPT_CUSTOMREQUEST ] = 'DOWNLOAD'; |
||||
759 | $this->curlOptions[ CURLOPT_BINARYTRANSFER ] = true; |
||||
760 | $this->curlOptions[ CURLOPT_RETURNTRANSFER ] = false; |
||||
761 | |||||
762 | return $this->getResponse(); |
||||
763 | } |
||||
764 | |||||
765 | // ------------------------------------------------------------------------ |
||||
766 | |||||
767 | /** |
||||
768 | * Request::getHandle |
||||
769 | * |
||||
770 | * Gets curl handle resource. |
||||
771 | * |
||||
772 | * @return resource |
||||
773 | */ |
||||
774 | public function getHandle() |
||||
775 | { |
||||
776 | if ($this->curlAutoClose) { |
||||
777 | $this->curlOptions[ CURLOPT_FORBID_REUSE ] = true; |
||||
778 | $this->curlOptions[ CURLOPT_FRESH_CONNECT ] = true; |
||||
779 | } |
||||
780 | |||||
781 | $this->curlOptions[ CURLOPT_URL ] = $this->uri->__toString(); |
||||
782 | |||||
783 | $handle = curl_init($this->uri->__toString()); |
||||
784 | |||||
785 | curl_setopt_array($handle, $this->curlOptions); |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_setopt_array() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
786 | |||||
787 | return $handle; |
||||
788 | } |
||||
789 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.