1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Http Options |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/shahradelahi/easy-http |
9
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
10
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
11
|
|
|
*/ |
12
|
|
|
class HttpOptions |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ?array |
17
|
|
|
*/ |
18
|
|
|
public ?array $headers = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ?array |
22
|
|
|
*/ |
23
|
|
|
public ?array $queries = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ?string |
27
|
|
|
*/ |
28
|
|
|
public ?string $body = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The proxy server to use |
32
|
|
|
* |
33
|
|
|
* @var ?ProxyServer |
34
|
|
|
*/ |
35
|
|
|
public ?ProxyServer $proxy = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add specific opt to curl |
39
|
|
|
* |
40
|
|
|
* @var ?array |
41
|
|
|
*/ |
42
|
|
|
public ?array $curlOptions = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The timeout of the request |
46
|
|
|
* |
47
|
|
|
* @var ?int |
48
|
|
|
*/ |
49
|
|
|
public ?int $timeout = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Http Options constructor. |
53
|
|
|
* |
54
|
|
|
* @param array $options |
55
|
|
|
*/ |
56
|
|
|
public function __construct(array $options = []) |
57
|
|
|
{ |
58
|
|
|
$this->setOptions($options); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the class as an array |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function toArray(): array |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'headers' => $this->headers, |
70
|
|
|
'queries' => $this->queries, |
71
|
|
|
'body' => $this->body, |
72
|
|
|
'proxy' => $this->proxy, |
73
|
|
|
'curlOptions' => $this->curlOptions, |
74
|
|
|
'timeout' => $this->timeout |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set Options |
80
|
|
|
* |
81
|
|
|
* @param array $options |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function setOptions(array $options): void |
85
|
|
|
{ |
86
|
|
|
foreach ($options as $key => $value) { |
87
|
|
|
if (method_exists($this, 'set' . ucfirst($key))) { |
88
|
|
|
if ($value !== null) { |
89
|
|
|
$this->{'set' . ucfirst($key)}($value); |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
|
|
if (property_exists($this, $key)) { |
93
|
|
|
$this->{$key} = $value; |
94
|
|
|
} else { |
95
|
|
|
throw new \InvalidArgumentException("Invalid option: $key"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set Body of Http request |
103
|
|
|
* |
104
|
|
|
* @param ?string|array $body The body of the request - On array it will be converted to json |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setBody(string|array|null $body): void |
108
|
|
|
{ |
109
|
|
|
if (is_array($body)) { |
|
|
|
|
110
|
|
|
$this->body = json_encode($body); |
111
|
|
|
$this->headers['Content-Type'] = 'application/json'; |
112
|
|
|
} else { |
113
|
|
|
$this->body = $body; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set proxy server |
119
|
|
|
* |
120
|
|
|
* @param array $proxy ["host", "port", "user", "pass"] |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function setProxy(array $proxy): void |
124
|
|
|
{ |
125
|
|
|
$this->proxy = (new ProxyServer())->setProxy($proxy); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get Query String |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getQueryString(): string |
134
|
|
|
{ |
135
|
|
|
return http_build_query($this->queries); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set Curl Options |
140
|
|
|
* |
141
|
|
|
* @param array $options [{"CURLOPT_*": "value"}, ...] |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function setCurlOptions(array $options): void |
145
|
|
|
{ |
146
|
|
|
if (count($options) > 0) { |
147
|
|
|
foreach ($options as $option => $value) { |
148
|
|
|
$this->curlOptions[$option] = $value; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |