forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.php
More file actions
167 lines (131 loc) · 4.59 KB
/
Copy pathHttpClient.php
File metadata and controls
167 lines (131 loc) · 4.59 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace b8;
class HttpClient
{
protected $_base = '';
protected $_params = [];
protected $_headers = [];
public function __construct($base = null)
{
$settings = Config::getInstance()->get('b8.http.client', ['base_url' => '', 'params' => []]);
$this->_base = $settings['base_url'];
$this->_params = isset($settings['params']) && is_array($settings['params']) ? $settings['params'] : [];
$this->_headers = ['Content-Type: application/x-www-form-urlencoded'];
if (!is_null($base)) {
$this->_base = $base;
}
}
public function setHeaders(array $headers)
{
$this->_headers = $headers;
}
public function request($method, $uri, $params = [])
{
// Clean incoming:
$method = strtoupper($method);
$getParams = $this->_params;
if ($method == 'GET' || $method == 'DELETE') {
$getParams = array_merge($getParams, $params);
} else {
$bodyParams = is_array($params) ? http_build_query($params) : $params;
}
$getParams = http_build_query($getParams);
if (substr($uri, 0, 1) != '/' && !empty($this->_base)) {
$uri = '/' . $uri;
}
// Build HTTP context array:
$context = [];
$context['http']['user_agent'] = 'b8/1.0';
$context['http']['timeout'] = 30;
$context['http']['method'] = $method;
$context['http']['ignore_errors'] = true;
$context['http']['header'] = implode(PHP_EOL, $this->_headers);
if (in_array($method, ['PUT', 'POST'])) {
$context['http']['content'] = $bodyParams;
}
$uri .= '?' . $getParams;
$context = stream_context_create($context);
$result = file_get_contents($this->_base . $uri, false, $context);
$res = [];
$res['headers'] = $http_response_header;
$res['code'] = (int)preg_replace('/HTTP\/1\.[0-1] ([0-9]+)/', '$1', $res['headers'][0]);
$res['success'] = false;
$res['body'] = $this->decodeResponse($result);
if ($res['code'] >= 200 && $res['code'] < 300) {
$res['success'] = true;
}
// Handle JSON responses:
foreach ($res['headers'] as $header) {
if (stripos($header, 'Content-Type') !== false || stripos($header, 'b8-Type') !== false) {
if (stripos($header, 'application/json') !== false) {
$res['text_body'] = $res['body'];
$res['body'] = json_decode($res['body'], true);
}
}
}
return $res;
}
public function get($uri, $params = [])
{
return $this->request('GET', $uri, $params);
}
public function put($uri, $params = [])
{
return $this->request('PUT', $uri, $params);
}
public function post($uri, $params = [])
{
return $this->request('POST', $uri, $params);
}
public function delete($uri, $params = [])
{
return $this->request('DELETE', $uri, $params);
}
protected function decodeResponse($originalResponse)
{
$response = $originalResponse;
$body = '';
do {
$line = $this->readChunk($response);
if ($line == PHP_EOL) {
continue;
}
$length = hexdec(trim($line));
if (!is_int($length) || empty($response) || $line === false || $length < 1) {
break;
}
do {
$data = $this->readChunk($response, $length);
// remove the amount received from the total length on the next loop
// it'll attempt to read that much less data
$length -= strlen($data);
// store in string for later use
$body .= $data;
// zero or less or end of connection break
if ($length <= 0 || empty($response)) {
break;
}
} while (true);
} while (true);
if (empty($body)) {
$body = $originalResponse;
}
return $body;
}
protected function readChunk(&$string, $len = 4096)
{
$rtn = '';
for ($i = 0; $i <= $len; $i++) {
if (empty($string)) {
break;
}
$char = $string[0];
$string = substr($string, 1);
$rtn .= $char;
if ($char == PHP_EOL) {
break;
}
}
return $rtn;
}
}