|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Curl request class |
|
4
|
|
|
* |
|
5
|
|
|
* @file CurlRequest.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 8.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Yancharuk Alexander <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2021 Alexander Yancharuk |
|
11
|
|
|
* @date 2015-01-20 18:12 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\CurlRequest; |
|
17
|
|
|
|
|
18
|
|
|
use Veles\CurlRequest\AuthStrategies\AuthStrategyInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class CurlRequest |
|
22
|
|
|
* @author Yancharuk Alexander <alex at itvault dot info> |
|
23
|
|
|
*/ |
|
24
|
|
|
class CurlRequest extends CurlAbstract |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Creates cURL handler and sets options |
|
28
|
|
|
* |
|
29
|
|
|
* @param $url |
|
30
|
|
|
* @param array $options |
|
31
|
|
|
*/ |
|
32
|
14 |
|
public function __construct($url, array $options = []) |
|
33
|
|
|
{ |
|
34
|
14 |
|
$this->curl = curl_init(); |
|
|
|
|
|
|
35
|
14 |
|
$options += $this->default_options; |
|
36
|
|
|
|
|
37
|
14 |
|
$this->setOption(CURLOPT_URL, $url) |
|
38
|
14 |
|
->setArrayOptions($options); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function __destruct() |
|
42
|
|
|
{ |
|
43
|
1 |
|
curl_close($this->curl); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Executes request and by default returns result as string |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function exec() |
|
52
|
|
|
{ |
|
53
|
1 |
|
return curl_exec($this->curl); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Sets additional header for request |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $headers Array of additional headers |
|
60
|
|
|
* |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function setHeaders(array $headers) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$this->options[CURLOPT_HTTPHEADER] = $headers; |
|
66
|
3 |
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); |
|
67
|
|
|
|
|
68
|
3 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get headers set for prepared request |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function getHeaders() |
|
77
|
|
|
{ |
|
78
|
3 |
|
return isset($this->options[CURLOPT_HTTPHEADER]) |
|
79
|
3 |
|
? $this->options[CURLOPT_HTTPHEADER] |
|
80
|
3 |
|
: []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set curl option |
|
85
|
|
|
* |
|
86
|
|
|
* @param $option |
|
87
|
|
|
* @param $value |
|
88
|
|
|
* |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
14 |
|
public function setOption($option, $value) |
|
92
|
|
|
{ |
|
93
|
14 |
|
$this->options[$option] = $value; |
|
94
|
14 |
|
curl_setopt($this->curl, $option, $value); |
|
95
|
|
|
|
|
96
|
14 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Sets additional options to curl-request |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $options |
|
103
|
|
|
* |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
14 |
|
public function setArrayOptions(array $options) |
|
107
|
|
|
{ |
|
108
|
14 |
|
foreach ($options as $option => $value) { |
|
109
|
14 |
|
$this->options[$option] = $value; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
14 |
|
curl_setopt_array($this->curl, $options); |
|
113
|
|
|
|
|
114
|
14 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set request authentication strategy |
|
119
|
|
|
* |
|
120
|
|
|
* @param AuthStrategyInterface $auth Authentication strategy |
|
121
|
|
|
* |
|
122
|
|
|
* @return $this |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function setAuth(AuthStrategyInterface $auth) |
|
125
|
|
|
{ |
|
126
|
1 |
|
$auth->apply($this); |
|
127
|
1 |
|
$this->auth = $auth; |
|
128
|
|
|
|
|
129
|
1 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
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 mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.