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