1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* sources. |
4
|
|
|
* Date: 14/08/15 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mailxpert\Http; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RawResponse |
11
|
|
|
* @package Mailxpert\Http |
12
|
|
|
*/ |
13
|
|
|
class RawResponse |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array The response headers in the form of an associative array. |
17
|
|
|
*/ |
18
|
|
|
protected $headers; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string The raw response body. |
22
|
|
|
*/ |
23
|
|
|
protected $body; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int The HTTP status response code. |
27
|
|
|
*/ |
28
|
|
|
protected $httpResponseCode; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creates a new RawResponse entity. |
32
|
|
|
* |
33
|
|
|
* @param string|array $headers The headers as a raw string or array. |
34
|
|
|
* @param string $body The raw response body. |
35
|
|
|
* @param int $httpStatusCode The HTTP response code (if sending headers as parsed array). |
36
|
|
|
*/ |
37
|
|
|
public function __construct($headers, $body, $httpStatusCode = null) |
38
|
|
|
{ |
39
|
|
|
if (is_numeric($httpStatusCode)) { |
40
|
|
|
$this->httpResponseCode = (int) $httpStatusCode; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (is_array($headers)) { |
44
|
|
|
$this->headers = $headers; |
45
|
|
|
} else { |
46
|
|
|
$this->setHeadersFromString($headers); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->body = $body; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return the response headers. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getHeaders() |
58
|
|
|
{ |
59
|
|
|
return $this->headers; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the body of the response. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getBody() |
68
|
|
|
{ |
69
|
|
|
return $this->body; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the HTTP response code. |
74
|
|
|
* |
75
|
|
|
* @return int |
76
|
|
|
*/ |
77
|
|
|
public function getHttpResponseCode() |
78
|
|
|
{ |
79
|
|
|
return $this->httpResponseCode; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the HTTP response code from a raw header. |
84
|
|
|
* |
85
|
|
|
* @param string $rawResponseHeader |
86
|
|
|
*/ |
87
|
|
|
public function setHttpResponseCodeFromHeader($rawResponseHeader) |
88
|
|
|
{ |
89
|
|
|
preg_match('/HTTP\/(\d\.\d|\d{1})\s+(\d+)\s+.*/', $rawResponseHeader, $match); |
90
|
|
|
$this->httpResponseCode = (int) $match[2]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Parse the raw headers and set as an array. |
95
|
|
|
* |
96
|
|
|
* @param string $rawHeaders The raw headers from the response. |
97
|
|
|
*/ |
98
|
|
|
protected function setHeadersFromString($rawHeaders) |
99
|
|
|
{ |
100
|
|
|
// Normalize line breaks |
101
|
|
|
$rawHeaders = str_replace("\r\n", "\n", $rawHeaders); |
102
|
|
|
|
103
|
|
|
// There will be multiple headers if a 301 was followed |
104
|
|
|
// or a proxy was followed, etc |
105
|
|
|
$headerCollection = explode("\n\n", trim($rawHeaders)); |
106
|
|
|
// We just want the last response (at the end) |
107
|
|
|
$rawHeader = array_pop($headerCollection); |
108
|
|
|
|
109
|
|
|
$headerComponents = explode("\n", $rawHeader); |
110
|
|
|
foreach ($headerComponents as $line) { |
111
|
|
|
if (strpos($line, ': ') === false) { |
112
|
|
|
$this->setHttpResponseCodeFromHeader($line); |
113
|
|
|
} else { |
114
|
|
|
list($key, $value) = explode(': ', $line); |
115
|
|
|
$this->headers[$key] = $value; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|