|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyHttp; |
|
4
|
|
|
|
|
5
|
|
|
use CurlHandle; |
|
6
|
|
|
use EasyHttp\Model\DownloadResult; |
|
7
|
|
|
use EasyHttp\Model\HttpOptions; |
|
8
|
|
|
use EasyHttp\Model\HttpResponse; |
|
9
|
|
|
use EasyHttp\Traits\ClientTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Client |
|
13
|
|
|
* |
|
14
|
|
|
* @link https://github.com/shahradelahi/easy-http |
|
15
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
|
16
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
|
17
|
|
|
*/ |
|
18
|
|
|
class Client |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
use ClientTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Set has self-signed certificate |
|
25
|
|
|
* |
|
26
|
|
|
* This is used to set the curl option CURLOPT_SSL_VERIFYPEER |
|
27
|
|
|
* and CURLOPT_SSL_VERIFYHOST to false. This is useful when you are |
|
28
|
|
|
* in local environment, or you have self-signed certificate. |
|
29
|
|
|
* |
|
30
|
|
|
* @param bool $has |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function setHasSelfSignedCertificate(bool $has): void |
|
34
|
|
|
{ |
|
35
|
|
|
define('EZ_CURL_SSL_SELF_SIGNED', $has); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* This method is used to send a http request to a given url. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $method |
|
42
|
|
|
* @param string $uri |
|
43
|
|
|
* @param array|HttpOptions $options |
|
44
|
|
|
* @return HttpResponse |
|
45
|
|
|
*/ |
|
46
|
|
|
public function request(string $method, string $uri, array|HttpOptions $options = []): HttpResponse |
|
47
|
|
|
{ |
|
48
|
|
|
$CurlHandle = $this->createCurlHandler($method, $uri, $options); |
|
49
|
|
|
|
|
50
|
|
|
curl_exec($CurlHandle); |
|
51
|
|
|
curl_close($CurlHandle); |
|
52
|
|
|
|
|
53
|
|
|
return (new HttpResponse())->setResponse([ |
|
54
|
|
|
'status' => curl_getinfo($CurlHandle, CURLINFO_HTTP_CODE), |
|
55
|
|
|
'body' => curl_exec($CurlHandle), |
|
56
|
|
|
'info' => curl_getinfo($CurlHandle), |
|
57
|
|
|
'error' => curl_error($CurlHandle), |
|
58
|
|
|
]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Send multiple requests to a given url. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $requests [{method, uri, options}, ...] |
|
65
|
|
|
* @return array<HttpResponse> |
|
66
|
|
|
*/ |
|
67
|
|
|
public function bulk(array $requests): array |
|
68
|
|
|
{ |
|
69
|
|
|
$result = []; |
|
70
|
|
|
$handlers = []; |
|
71
|
|
|
$multi_handler = curl_multi_init(); |
|
72
|
|
|
foreach ($requests as $request) { |
|
73
|
|
|
|
|
74
|
|
|
$CurlHandle = $this->createCurlHandler( |
|
75
|
|
|
$request['method'], |
|
76
|
|
|
$request['uri'], |
|
77
|
|
|
$request['options'] ?? [] |
|
78
|
|
|
); |
|
79
|
|
|
$handlers[] = $CurlHandle; |
|
80
|
|
|
curl_multi_add_handle($multi_handler, $CurlHandle); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$active = null; |
|
85
|
|
|
do { |
|
86
|
|
|
$mrc = curl_multi_exec($multi_handler, $active); |
|
|
|
|
|
|
87
|
|
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($handlers as $handler) { |
|
90
|
|
|
curl_multi_remove_handle($multi_handler, $handler); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
curl_multi_close($multi_handler); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
foreach ($handlers as $handler) { |
|
95
|
|
|
$result[] = (new HttpResponse())->setResponse([ |
|
96
|
|
|
'status' => curl_getinfo($handler, CURLINFO_HTTP_CODE), |
|
97
|
|
|
'body' => curl_exec($handler), |
|
98
|
|
|
'info' => curl_getinfo($handler), |
|
99
|
|
|
'error' => curl_error($handler), |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $result; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Create curl handler. |
|
108
|
|
|
* |
|
109
|
|
|
* @param ?string $method |
|
110
|
|
|
* @param string $uri |
|
111
|
|
|
* @param array|HttpOptions $options |
|
112
|
|
|
* @return ?CurlHandle |
|
113
|
|
|
*/ |
|
114
|
|
|
private function createCurlHandler(?string $method, string $uri, array|HttpOptions $options = []): ?CurlHandle |
|
115
|
|
|
{ |
|
116
|
|
|
$cHandler = curl_init(); |
|
117
|
|
|
|
|
118
|
|
|
if (gettype($options) === 'array') { |
|
119
|
|
|
$options = new HttpOptions( |
|
120
|
|
|
$this->getOptions($options) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (count($options->queries) > 0) { |
|
125
|
|
|
if (!str_contains($uri, '?')) $uri .= '?'; |
|
126
|
|
|
$uri .= $options->getQueryString(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
curl_setopt($cHandler, CURLOPT_URL, $uri); |
|
130
|
|
|
|
|
131
|
|
|
$this->setCurlOpts($cHandler, $method, $options); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
return $cHandler; |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Setup curl options based on the given method and our options. |
|
138
|
|
|
* |
|
139
|
|
|
* @param CurlHandle $cHandler |
|
140
|
|
|
* @param ?string $method |
|
141
|
|
|
* @param HttpOptions $options |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
|
|
private function setCurlOpts(CurlHandle $cHandler, ?string $method, HttpOptions $options): void |
|
145
|
|
|
{ |
|
146
|
|
|
curl_setopt($cHandler, CURLOPT_HEADER, true); |
|
147
|
|
|
curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'GET'); |
|
148
|
|
|
|
|
149
|
|
|
# Fetch the header |
|
150
|
|
|
$fetchedHeaders = []; |
|
151
|
|
|
foreach ($options->headers as $header => $value) { |
|
152
|
|
|
$fetchedHeaders[] = $header . ': ' . $value; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
# Set headers |
|
156
|
|
|
if ($fetchedHeaders != []) { |
|
157
|
|
|
curl_setopt($cHandler, CURLOPT_HTTPHEADER, $fetchedHeaders); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
# Add body if we have one. |
|
161
|
|
|
if ($options->body) { |
|
162
|
|
|
curl_setopt($cHandler, CURLOPT_CUSTOMREQUEST, $method ?? 'POST'); |
|
163
|
|
|
curl_setopt($cHandler, CURLOPT_POSTFIELDS, $options->body); |
|
164
|
|
|
curl_setopt($cHandler, CURLOPT_POST, true); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
# Check for a proxy |
|
168
|
|
|
if ($options->proxy != null) { |
|
169
|
|
|
curl_setopt($cHandler, CURLOPT_PROXY, $options->proxy->getProxy()); |
|
170
|
|
|
curl_setopt($cHandler, CURLOPT_PROXYUSERPWD, $options->proxy->getAuth()); |
|
171
|
|
|
if ($options->proxy->type !== null) { |
|
172
|
|
|
curl_setopt($cHandler, CURLOPT_PROXYTYPE, $options->proxy->type); |
|
173
|
|
|
curl_setopt($cHandler, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
curl_setopt($cHandler, CURLOPT_RETURNTRANSFER, true); |
|
178
|
|
|
curl_setopt($cHandler, CURLOPT_FOLLOWLOCATION, true); |
|
179
|
|
|
|
|
180
|
|
|
# Add and override the custom curl options. |
|
181
|
|
|
if (count($options->curlOptions) > 0) { |
|
|
|
|
|
|
182
|
|
|
foreach ($options->curlOptions as $option => $value) { |
|
183
|
|
|
curl_setopt($cHandler, $option, $value); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
# if we have a timeout, set it. |
|
188
|
|
|
if ($options->timeout != null) { |
|
|
|
|
|
|
189
|
|
|
curl_setopt($cHandler, CURLOPT_TIMEOUT, $options->timeout); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
# If self-signed certs are allowed, set it. |
|
193
|
|
|
if (EZ_CURL_SSL_SELF_SIGNED === true) { |
|
194
|
|
|
curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, false); |
|
195
|
|
|
curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Initialize options from array. |
|
201
|
|
|
* |
|
202
|
|
|
* @param array $options |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
|
|
private function getOptions(array $options): array |
|
206
|
|
|
{ |
|
207
|
|
|
$defaults = [ |
|
208
|
|
|
'headers' => [], |
|
209
|
|
|
'body' => null, |
|
210
|
|
|
'timeout' => null, |
|
211
|
|
|
'proxy' => null, |
|
212
|
|
|
'curlOptions' => [], |
|
213
|
|
|
'queries' => [] |
|
214
|
|
|
]; |
|
215
|
|
|
|
|
216
|
|
|
return array_merge($defaults, $options); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Download large files. |
|
221
|
|
|
* |
|
222
|
|
|
* This method is used to download large files with |
|
223
|
|
|
* creating multiple requests. |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $url The direct url to the file. |
|
226
|
|
|
* @param string $path The path to save the file. |
|
227
|
|
|
* @param array|HttpOptions $options The options to use. |
|
228
|
|
|
* |
|
229
|
|
|
* @return DownloadResult |
|
230
|
|
|
*/ |
|
231
|
|
|
public function download(string $url, string $path, array|HttpOptions $options = []): DownloadResult |
|
|
|
|
|
|
232
|
|
|
{ |
|
233
|
|
|
return new DownloadResult(); // TODO: Implement download() method. |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
} |