-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClient.php
56 lines (44 loc) · 1.59 KB
/
HttpClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace FsDeliverySdk;
class HttpClient
{
/** @var string */
private $url;
/** @var int */
private $timeout = 60;
public $headers = [];
public $http_status_code = 0;
public function __construct($url, $timeout = 60) {
$this->url = $url;
$this->timeout = $timeout;
}
public function get($method, $params = [], $headers = []) {
return $this->request('GET', $method, $params, $headers);
}
public function post($method, $params = [], $headers = []) {
return $this->request('POST', $method, $params, $headers);
}
private function request($method, $url, $params = [], $headers = []) {
$curlHeaders = [];
$this->http_status_code = 0;
$this->headers = [];
foreach ($headers as $header => $value) {
$curlHeaders[] = $header.': '.$value;
}
if ($method == 'GET' && !empty($params))
$url .= '?'.http_build_query($params);
$curl = curl_init($this->url.$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
}
$response = curl_exec($curl);
$this->headers = curl_getinfo($curl);
$this->http_status_code = !empty($this->headers['http_code']) ? $this->headers['http_code'] : 0;
curl_close($curl);
return $response;
}
}