1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\Curl; |
4
|
|
|
|
5
|
|
|
class Response |
6
|
|
|
{ |
7
|
|
|
/** @var Request */ |
8
|
|
|
private $request; |
9
|
|
|
|
10
|
|
|
/** @var string */ |
11
|
|
|
private $headers; |
12
|
|
|
/** @var string * */ |
13
|
|
|
private $content; |
14
|
|
|
/** @var array * */ |
15
|
|
|
private $info; |
16
|
|
|
|
17
|
21 |
|
public static function get(Request $request) |
18
|
|
|
{ |
19
|
21 |
|
$handle = $request->getHandle(); |
20
|
21 |
|
$returnHeaders = $request->getReturnHeader(); |
21
|
|
|
|
22
|
21 |
|
$content = curl_exec($handle); |
23
|
|
|
|
24
|
21 |
|
if (!$content) { |
25
|
12 |
|
return curl_errno($handle); |
26
|
|
|
} |
27
|
|
|
|
28
|
18 |
|
$self = new self($request); |
29
|
|
|
|
30
|
18 |
|
if (Request::RETURN_HEADER_ONLY === $returnHeaders) { |
31
|
|
|
$self->headers = $content; |
32
|
|
|
} else { |
33
|
18 |
|
if (Request::RETURN_HEADER === $returnHeaders) { // Remove headers from response |
34
|
18 |
|
$self->headers = substr($content, 0, $sHeaders = curl_getinfo($handle, CURLINFO_HEADER_SIZE)); |
35
|
18 |
|
$content = substr($content, $sHeaders); |
36
|
|
|
} |
37
|
|
|
|
38
|
18 |
|
$self->content = $content; |
39
|
|
|
} |
40
|
|
|
|
41
|
18 |
|
$self->info = curl_getinfo($handle); // curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL) |
42
|
|
|
|
43
|
18 |
|
return $self; |
44
|
|
|
} |
45
|
|
|
|
46
|
18 |
|
private function __construct(Request $request) |
47
|
|
|
{ |
48
|
18 |
|
$this->request = $request; |
49
|
18 |
|
} |
50
|
|
|
|
51
|
3 |
|
public function getRequest() |
52
|
|
|
{ |
53
|
3 |
|
return $this->request; |
54
|
|
|
} |
55
|
|
|
|
56
|
15 |
|
public function getContent() |
57
|
|
|
{ |
58
|
15 |
|
return $this->content; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return headers's data return by the request. |
63
|
|
|
* |
64
|
|
|
* @param bool $returnArray True to get an array, false to get a string |
65
|
|
|
* |
66
|
|
|
* @return array|string|null containing headers's data |
67
|
|
|
*/ |
68
|
6 |
|
public function getHeaders(bool $returnArray = true) |
69
|
|
|
{ |
70
|
6 |
|
if (isset($this->headers)) { |
71
|
6 |
|
return true === $returnArray ? Helper::httpParseHeaders($this->headers) : $this->headers; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string requested url |
77
|
|
|
*/ |
78
|
3 |
|
public function getUrl() |
79
|
|
|
{ |
80
|
3 |
|
return $this->request->getUrl(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return current effective url. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
18 |
|
public function getEffectiveUrl() |
89
|
|
|
{ |
90
|
18 |
|
return isset($this->info['url']) ? $this->info['url'] : null; //curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the cookie(s) returned by the request (if there are). |
95
|
|
|
* |
96
|
|
|
* @return array|null containing the cookies |
97
|
|
|
*/ |
98
|
|
|
public function getCookies() |
99
|
|
|
{ |
100
|
|
|
if (isset($this->headers)) { |
101
|
|
|
$headers = $this->getHeaders(); |
102
|
|
|
if (isset($headers['Set-Cookie'])) { |
103
|
|
|
if (is_array($headers['Set-Cookie'])) { |
104
|
|
|
return implode('; ', $headers['Set-Cookie']); |
105
|
|
|
} else { |
106
|
|
|
return $headers['Set-Cookie']; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get information regarding the request. |
114
|
|
|
* |
115
|
|
|
* @param string $key to get |
116
|
|
|
* |
117
|
|
|
* @return string|array |
118
|
|
|
*/ |
119
|
|
|
public function getInfo(?string $key = null) |
120
|
|
|
{ |
121
|
|
|
return $key ? (isset($this->info[$key]) ? $this->info[$key] : null) : $this->info; |
122
|
|
|
} |
123
|
|
|
|
124
|
12 |
|
public function getStatusCode() |
125
|
|
|
{ |
126
|
12 |
|
return $this->info['http_code']; |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
public function getContentType() |
130
|
|
|
{ |
131
|
6 |
|
return $this->info['content_type']; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|