1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\Curl; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class Response |
8
|
|
|
{ |
9
|
|
|
/** @var Request */ |
10
|
|
|
protected $request; |
11
|
|
|
|
12
|
|
|
protected string $headers = ''; |
13
|
|
|
|
14
|
|
|
/** @var string * */ |
15
|
|
|
protected string $content; |
16
|
|
|
|
17
|
36 |
|
/** @var array<string, int|string> an associative array with the following elements (which correspond to opt): "url" "content_type" "http_code" "header_size" "request_size" "filetime" "ssl_verify_result" "redirect_count" "total_time" "namelookup_time" "connect_time" "pretransfer_time" "size_upload" "size_download" "speed_download" "speed_upload" "download_content_length" "upload_content_length" "starttransfer_time" "redirect_time" */ |
18
|
|
|
protected $info; |
19
|
36 |
|
|
20
|
|
|
/** |
21
|
36 |
|
* @return self|int |
22
|
|
|
* @psalm-suppress InvalidArgument (for $handle) |
23
|
36 |
|
*/ |
24
|
18 |
|
public static function get(Request $request) |
25
|
|
|
{ |
26
|
|
|
$handle = $request->getHandle(); |
27
|
27 |
|
|
28
|
|
|
$content = curl_exec($handle); |
29
|
27 |
|
|
30
|
3 |
|
if (false === $content) { |
31
|
|
|
return curl_errno($handle); |
32
|
24 |
|
} |
33
|
18 |
|
|
34
|
18 |
|
if (true === $content) { |
35
|
|
|
throw new Exception('CURLOPT_RETURNTRANSFER and CURLOPT_HEADER was set to 0.'); |
36
|
|
|
} |
37
|
24 |
|
|
38
|
|
|
$self = new self($request); |
39
|
|
|
|
40
|
27 |
|
if (Request::RETURN_HEADER_ONLY === $request->mustReturnHeaders()) { |
41
|
|
|
$self->headers = $content; |
42
|
27 |
|
} else { |
43
|
|
|
if (Request::RETURN_HEADER === $request->mustReturnHeaders()) { // Remove headers from response |
44
|
|
|
$self->headers = substr($content, 0, $sHeaders = (int) $request->getRequestInfo(\CURLINFO_HEADER_SIZE)); |
45
|
27 |
|
$content = substr($content, $sHeaders); |
46
|
|
|
} |
47
|
27 |
|
|
48
|
27 |
|
$self->content = $content; |
49
|
|
|
} |
50
|
6 |
|
|
51
|
|
|
$self->info = $request->getRequestInfos(); |
52
|
6 |
|
|
53
|
|
|
return $self; |
54
|
|
|
} |
55
|
21 |
|
|
56
|
|
|
private function __construct(Request $request) |
57
|
21 |
|
{ |
58
|
|
|
$this->request = $request; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getRequest(): ?Request |
62
|
|
|
{ |
63
|
|
|
return $this->request; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getContent(): string |
67
|
15 |
|
{ |
68
|
|
|
return $this->content; |
69
|
15 |
|
} |
70
|
15 |
|
|
71
|
|
|
/** |
72
|
|
|
* Return headers's data return by the request. |
73
|
|
|
* |
74
|
|
|
* @return ?array<int|string, string|string[]> containing headers's data |
75
|
|
|
*/ |
76
|
|
|
public function getHeaders(): ?array |
77
|
3 |
|
{ |
78
|
|
|
if ('' === $this->headers) { |
79
|
3 |
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$parsed = Helper::httpParseHeaders($this->headers); |
83
|
|
|
if ([] === $parsed) { |
84
|
|
|
throw new Exception('Failed to parse Headers `'.$this->headers.'`'); |
85
|
|
|
} |
86
|
|
|
|
87
|
27 |
|
return $parsed; |
88
|
|
|
} |
89
|
27 |
|
|
90
|
|
|
public function getRawHeaders(): string |
91
|
|
|
{ |
92
|
|
|
return $this->headers; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string requested url |
97
|
3 |
|
*/ |
98
|
|
|
public function getUrl(): string |
99
|
3 |
|
{ |
100
|
3 |
|
return $this->request->getUrl(); |
101
|
3 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Return current effective url. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getEffectiveUrl(): ?string |
109
|
3 |
|
{ |
110
|
|
|
return isset($this->info['url']) ? (string) $this->info['url'] : null; |
111
|
|
|
//curl_getinfo(self::$ch, CURLINFO_EFFECTIVE_URL); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return the cookie(s) returned by the request (if there are). |
116
|
|
|
* |
117
|
|
|
* @return string|null containing the cookies |
118
|
6 |
|
*/ |
119
|
|
|
public function getCookies() |
120
|
6 |
|
{ |
121
|
|
|
$headers = $this->getHeaders(); |
122
|
|
|
if (null !== $headers && isset($headers['Set-Cookie'])) { |
123
|
12 |
|
if (\is_array($headers['Set-Cookie'])) { |
124
|
|
|
return implode('; ', $headers['Set-Cookie']); |
125
|
12 |
|
} else { |
126
|
|
|
return $headers['Set-Cookie']; |
127
|
|
|
} |
128
|
6 |
|
} |
129
|
|
|
|
130
|
6 |
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
6 |
|
/** |
134
|
|
|
* Get information regarding the request. |
135
|
6 |
|
* |
136
|
|
|
* @param string $key to get |
137
|
6 |
|
* |
138
|
|
|
* @return int|string|array<string, string|int>|null |
139
|
|
|
*/ |
140
|
|
|
public function getInfo(?string $key = null) |
141
|
|
|
{ |
142
|
|
|
return $key ? (isset($this->info[$key]) ? $this->info[$key] : null) : $this->info; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getStatusCode(): int |
146
|
|
|
{ |
147
|
|
|
return (int) $this->info['http_code']; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getContentType(): string |
151
|
|
|
{ |
152
|
|
|
return (string) $this->info['content_type']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getMimeType(): ?string |
156
|
|
|
{ |
157
|
|
|
$headers = Helper::parseHeader($this->getContentType()); |
158
|
|
|
|
159
|
|
|
return $headers[0][0] ?? null; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|