1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 namespace Slimore\Http;
15
16 17 18 19 20 21
22
23 class Client
24 {
25 26 27
28 const HEAD = 'HEAD';
29 const GET = 'GET';
30 const POST = 'POST';
31 const PUT = 'PUT';
32 const DELETE = 'DELETE';
33 const PATCH = 'PATCH';
34 const OPTIONS = 'OPTIONS';
35 const TRACE = 'TRACE';
36
37 38 39
40
41 42 43
44 public $url;
45
46 47 48
49 public $header = false;
50
51 52 53
54 public $timeout = 1200;
55
56 57 58
59 public $headers = ['X-Framework-By: Slimore/0.1.0'];
60
61 62 63
64 public $fileTime = true;
65
66 67 68
69 public $nosignal = true;
70
71 72 73
74 public $userAgent = 'Slimore Http client';
75
76 77 78
79 public $freshConnect = false;
80
81 82 83
84 public $sslVerifyPeer = false;
85
86 87 88
89 public $sslVerifyHost = false;
90
91 92 93
94 public $connectTimeout = 1200;
95
96 97 98
99 public $returnTransfer = true;
100
101 102 103
104 private $curl;
105
106 107 108
109 public $info;
110
111 112 113
114 public $errors = [];
115
116 117 118
119 public $options = [];
120
121 122 123
124 public $response;
125
126 127 128
129
130 public function __construct()
131 {
132 $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
133 }
134
135 136 137 138 139 140
141
142 public function curl($url = null)
143 {
144 $this->curl = curl_init($url);
145 }
146
147 148 149 150 151
152
153 public function setDefaultOptions()
154 {
155 curl_setopt_array($this->curl, [
156 CURLOPT_TIMEOUT => $this->timeout,
157 CURLOPT_NOSIGNAL => $this->nosignal,
158 CURLOPT_FILETIME => $this->fileTime,
159 CURLOPT_USERAGENT => $this->userAgent,
160 CURLOPT_HEADER => $this->header,
161 CURLOPT_HTTPHEADER => $this->headers,
162 CURLOPT_RETURNTRANSFER => $this->returnTransfer,
163 CURLOPT_SSL_VERIFYPEER => $this->sslVerifyPeer,
164 CURLOPT_SSL_VERIFYHOST => $this->sslVerifyHost,
165 CURLOPT_FRESH_CONNECT => $this->freshConnect
166 ]);
167 }
168
169 170 171 172 173 174 175
176
177 public function setOption($option, $value)
178 {
179 curl_setopt($this->curl, $option, $value);
180 }
181
182 183 184 185 186
187
188 public function execute()
189 {
190 $this->response = curl_exec($this->curl);
191
192 return $this->response;
193 }
194
195 196 197 198 199 200
201
202 public function method($method)
203 {
204 $this->headers = array_merge($this->headers, array('X-HTTP-Method-Override: ' . $method));
205 $this->setOption(CURLOPT_CUSTOMREQUEST, $method);
206 $this->setOption(CURLOPT_HTTPHEADER, $this->headers);
207 }
208
209 210 211 212 213
214
215 public function close()
216 {
217 curl_close($this->curl);
218 }
219
220 221 222 223 224
225
226 public function error()
227 {
228 $this->errors = [
229 'code' => curl_errno($this->curl),
230 'message' => curl_error($this->curl)
231 ];
232
233 return $this->error();
234 }
235
236 237 238 239 240
241
242 public function getInfo()
243 {
244 $this->info = curl_getinfo($this->curl);
245
246 return $this->info;
247 }
248
249 250 251 252 253 254 255 256 257
258
259 public function send($url, $method = 'GET', $data = null, callable $callback = null)
260 {
261 $this->curl($url);
262 $this->setDefaultOptions();
263
264 if (is_array($this->options))
265 {
266 if (count($this->options) > 0)
267 {
268 foreach ($this->options as $key => $value)
269 {
270 $this->setOption($key, $value);
271 }
272 }
273 }
274
275 if ($method !== self::GET)
276 {
277 $this->method($method);
278 }
279
280 if ($method === self::POST || $method === self::PUT || $method === self::DELETE)
281 {
282 $this->setOption(CURLOPT_POSTFIELDS, $data);
283 }
284
285 $response = $this->execute();
286
287 if (!$response)
288 {
289 $this->error();
290 }
291
292 $this->getInfo();
293 $this->close();
294
295 if ( is_callable($callback) )
296 {
297 $callback($this->response, $this->info, $this->errors);
298 }
299
300 return $response;
301 }
302
303 304 305 306 307 308 309
310
311 public function head($url, $fields = null, callable $callback = null)
312 {
313 $this->send($url, self::HEAD, $fields, $callback);
314 }
315
316 317 318 319 320 321 322 323
324
325 public function get($url, $queries = null, callable $callback = null)
326 {
327 if ( is_array($queries) )
328 {
329 $queries = '?' . http_build_query($queries);
330 }
331
332 return $this->send($url . $queries, self::GET, [], $callback);
333 }
334
335 336 337 338 339 340 341 342
343
344 public function post($url, array $fields = [], callable $callback = null)
345 {
346 return $this->send($url, self::POST, $fields, $callback);
347 }
348
349 350 351 352 353 354 355 356
357
358 public function put($url, $fields = null, callable $callback = null)
359 {
360 $fields = (is_array($fields)) ? http_build_query($fields) : $fields;
361
362 return $this->send($url, self::PUT, $fields, $callback);
363 }
364
365 366 367 368 369 370 371 372
373
374 public function delete($url, $fields = null, callable $callback = null)
375 {
376 $fields = ( is_array($fields) ) ? http_build_query($fields) : $fields;
377
378 return $this->send($url, self::DELETE, $fields, $callback);
379 }
380
381 382 383 384 385 386 387
388
389 public function patch($url, $fields = null, callable $callback = null)
390 {
391 $this->send($url, self::PATCH, $fields, $callback);
392 }
393
394 395 396 397 398 399 400
401 public function options($url, $fields = null, callable $callback = null)
402 {
403 $this->send($url, self::OPTIONS, $fields, $callback);
404 }
405
406 407 408 409 410 411 412
413
414 public function trace($url, $fields = null, callable $callback = null)
415 {
416 $this->send($url, self::TRACE, $fields, $callback);
417 }
418 }