|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* spindle/httpclient |
|
4
|
|
|
* @license CC0-1.0 (Public Domain) https://creativecommons.org/publicdomain/zero/1.0/ |
|
5
|
|
|
* @see https://github.com/spindle/spindle-httpclient |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spindle\HttpClient; |
|
8
|
|
|
|
|
9
|
|
|
class Response { |
|
10
|
|
|
protected |
|
11
|
|
|
$body = '' |
|
12
|
|
|
, $header = '' |
|
13
|
|
|
, $info |
|
14
|
|
|
, $headerCache |
|
15
|
|
|
; |
|
16
|
|
|
|
|
17
|
4 |
|
function __construct($body, array $info) { |
|
|
|
|
|
|
18
|
4 |
|
if (is_string($body)) { |
|
19
|
3 |
|
$this->header = substr($body, 0, $info['header_size']); |
|
20
|
3 |
|
$this->body = substr($body, $info['header_size']); |
|
21
|
3 |
|
} |
|
22
|
4 |
|
$this->info = $info; |
|
23
|
4 |
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
function __toString() { |
|
|
|
|
|
|
26
|
1 |
|
return $this->getBody(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
function getHeaderString() { |
|
|
|
|
|
|
30
|
1 |
|
return $this->header; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
function getHeader($label = null) { |
|
|
|
|
|
|
34
|
1 |
|
if (! $this->headerCache) { |
|
35
|
1 |
|
$headerString = rtrim($this->header); |
|
36
|
1 |
|
$headerString = str_replace(array("\r\n", "\r"), "\n", $headerString); |
|
37
|
1 |
|
$headerArr = explode("\n", $headerString); |
|
38
|
1 |
|
array_shift($headerArr); |
|
39
|
|
|
|
|
40
|
1 |
|
$result = array(); |
|
41
|
1 |
|
foreach ($headerArr as $h) { |
|
42
|
1 |
|
$pos = strpos($h, ':'); |
|
43
|
1 |
|
$key = substr($h, 0, $pos); |
|
44
|
1 |
|
$value = substr($h, $pos+1); |
|
45
|
1 |
|
$result[trim($key)] = trim($value); |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$this->headerCache = $result; |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
if ($label) { |
|
52
|
1 |
|
return isset($this->headerCache[$label]) ? $this->headerCache[$label] : null; |
|
53
|
|
|
} else { |
|
54
|
1 |
|
return $this->headerCache; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
function getUrl() { |
|
|
|
|
|
|
59
|
1 |
|
return $this->info['url']; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
function getStatusCode() { |
|
|
|
|
|
|
63
|
1 |
|
return $this->info['http_code']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
function getContentType() { |
|
|
|
|
|
|
67
|
1 |
|
return $this->info['content_type']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
function getContentLength() { |
|
|
|
|
|
|
71
|
1 |
|
return $this->info['download_content_length']; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
function getInfo($label = null) { |
|
|
|
|
|
|
75
|
1 |
|
if ($label && isset($this->info[$label])) { |
|
76
|
1 |
|
return $this->info[$label]; |
|
77
|
|
|
} |
|
78
|
1 |
|
return $this->info; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
function getBody() { |
|
|
|
|
|
|
82
|
3 |
|
return $this->body; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.