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