Response.php
3.46 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* PHP OOP cURL
*
* @author Andreas Lutro <anlutro@gmail.com>
* @license http://opensource.org/licenses/MIT
* @package PHP cURL
*/
namespace anlutro\cURL;
/**
* cURL response representation class.
*/
class Response
{
/**
* The response headers.
*
* @var array
*/
public $headers = array();
/**
* The response body.
*
* @var string
*/
public $body;
/**
* The results of curl_getinfo on the response request.
*
* @var array|false
*/
public $info;
/**
* The response code including text, e.g. '200 OK'.
*
* @var string
*/
public $statusText;
/**
* The response code.
*
* @var int
*/
public $statusCode;
/**
* @param string $body
* @param string $headers
* @param mixed $info
*/
public function __construct($body, $headers, $info = array())
{
$this->body = $body;
$this->info = $info;
$this->parseHeader($headers);
}
/**
* Parse a header string.
*
* @param string $header
*
* @return void
*/
protected function parseHeader($header)
{
$headers = explode("\r\n", trim($header));
$this->parseHeaders($headers);
}
/**
* Parse an array of headers.
*
* @param array $headers
*
* @return void
*/
protected function parseHeaders(array $headers)
{
$this->headers = array();
// find and set the HTTP status code and reason
$firstHeader = array_shift($headers);
if (!preg_match('/^HTTP\/\d(\.\d)? [0-9]{3}/', $firstHeader)) {
throw new \InvalidArgumentException('Invalid response header');
}
list(, $status) = explode(' ', $firstHeader, 2);
$code = explode(' ', $status);
$code = (int) $code[0];
// special handling for HTTP 100 responses
if ($code === 100) {
// remove empty header lines between 100 and actual HTTP status
foreach ($headers as $key => $header) {
if ($header) {
break;
}
unset($headers[$key]);
}
// start the process over with the 100 continue header stripped away
return $this->parseHeaders($headers);
}
$this->statusText = $status;
$this->statusCode = $code;
foreach ($headers as $header) {
// skip empty lines
if (!$header) {
continue;
}
$delimiter = strpos($header, ':');
if (!$delimiter) {
continue;
}
$key = trim(strtolower(substr($header, 0, $delimiter)));
$val = ltrim(substr($header, $delimiter + 1));
if (isset($this->headers[$key])) {
if (is_array($this->headers[$key])) {
$this->headers[$key][] = $val;
} else {
$this->headers[$key] = array($this->headers[$key], $val);
}
} else {
$this->headers[$key] = $val;
}
}
}
/**
* Get a specific header from the response.
*
* @param string $key
*
* @return mixed
*/
public function getHeader($key)
{
$key = strtolower($key);
return array_key_exists($key, $this->headers) ?
$this->headers[$key] : null;
}
/**
* Gets all the headers of the response.
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Convert the response instance to an array.
*
* @return array
*/
public function toArray()
{
return array(
'headers' => $this->headers,
'body' => $this->body,
'info' => $this->info
);
}
/**
* Convert the response object to a JSON string.
*
* @return string
*/
public function toJson()
{
return json_encode($this->toArray());
}
/**
* Convert the object to its string representation by returning the body.
*
* @return string
*/
public function __toString()
{
return $this->body;
}
}