1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dHttp; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* dHttp - http client based curl |
7
|
|
|
* |
8
|
|
|
* @author Askar Fuzaylov <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Response |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $_raw; |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $_headers = []; |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $_body; |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $_errors = []; |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $_info = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
* |
36
|
|
|
* @param array $response |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $response) |
39
|
|
|
{ |
40
|
|
|
$this->_raw = $response['response']; |
41
|
|
|
$this->_info = $response['info']; |
42
|
|
|
|
43
|
|
|
// Separate body a from a header |
44
|
|
|
if (isset($response['options'][CURLOPT_HEADER]) && $response['options'][CURLOPT_HEADER]) { |
45
|
|
|
list($headers, $this->_body) = explode("\r\n\r\n", $response['response'], 2); |
46
|
|
|
// Parse headers |
47
|
|
|
$this->parseHeaders($headers); |
48
|
|
|
} else { |
49
|
|
|
$this->_body = $response['response']; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return raw response |
55
|
|
|
* |
56
|
|
|
* @return null|string |
57
|
|
|
*/ |
58
|
|
|
public function getRaw() |
59
|
|
|
{ |
60
|
|
|
return $this->_raw; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return response headers |
65
|
|
|
* |
66
|
|
|
* @return null|string |
67
|
|
|
*/ |
68
|
|
|
public function getHeaders() |
69
|
|
|
{ |
70
|
|
|
return $this->_headers; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return response headers |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @param string $default |
78
|
|
|
* @return null|string |
79
|
|
|
*/ |
80
|
|
|
public function getHeader($name, $default = null) |
81
|
|
|
{ |
82
|
|
|
return array_key_exists($name, $this->_headers) ? $this->_headers[$name] : $default; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return response body |
87
|
|
|
* |
88
|
|
|
* @return null|string |
89
|
|
|
*/ |
90
|
|
|
public function getBody() |
91
|
|
|
{ |
92
|
|
|
return $this->_body; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set errors |
97
|
|
|
* |
98
|
|
|
* @param array $errors |
99
|
|
|
*/ |
100
|
|
|
public function setError($errors) |
101
|
|
|
{ |
102
|
|
|
$this->_errors = $errors; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return request errors |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getErrors() |
111
|
|
|
{ |
112
|
|
|
return $this->_errors; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return request errors |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function getCode() |
121
|
|
|
{ |
122
|
|
|
return $this->_info['http_code']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function isOk() |
129
|
|
|
{ |
130
|
|
|
return $this->_info['http_code'] == 200; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get access for properties |
135
|
|
|
* |
136
|
|
|
* @param string $name |
137
|
|
|
* @param array @params |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function __call($name, $params) |
141
|
|
|
{ |
142
|
|
|
$name = strtolower(str_replace('get', '', $name)); |
143
|
|
|
if (array_key_exists($name, $this->_info)) { |
144
|
|
|
return $this->_info[$name]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Parse headers |
152
|
|
|
* @param $headers |
153
|
|
|
*/ |
154
|
|
|
private function parseHeaders($headers) |
155
|
|
|
{ |
156
|
|
|
$exploded = explode("\r\n", $headers); |
157
|
|
|
foreach ($exploded as $headerString) { |
158
|
|
|
if (strpos($headerString, ':') !== false) { |
159
|
|
|
list($key, $val) = explode(':', $headerString, 2); |
160
|
|
|
$this->_headers[trim($key)] = trim($val); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|