PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.93
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.93
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Http / Response.php

Response.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.93, at vendor/wpfluent/framework/src/WPFluent/Http/Response.php

222 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Http;
4
5 /**
6 * Class Response
7 *
8 * Handles HTTP responses for HTTP Client class,
9 * providing methods to access response data.
10 */
11 class Response
12 {
13 /**
14 * The HTTP response data.
15 *
16 * @var array
17 */
18 protected $response;
19
20 /**
21 * Create a new Response instance.
22 *
23 * @param array $response The response data.
24 */
25 public function __construct($response)
26 {
27 $this->response = $response;
28 }
29
30 /**
31 * Convert the response to an array.
32 *
33 * @return array
34 */
35 public function toArray()
36 {
37 return $this->response;
38 }
39
40 /**
41 * Get the HTTP response object.
42 *
43 * @return mixed
44 */
45 public function getHttpResponseObject()
46 {
47 return $this->response['http_response'];
48 }
49
50 /**
51 * Determine if the response is successful (2xx status code).
52 *
53 * @return bool
54 */
55 public function isOkay()
56 {
57 return str_starts_with($this->getCode(), '2');
58 }
59
60 /**
61 * Determine if the response is a redirect (3xx status code).
62 *
63 * @return bool
64 */
65 public function isRedirect()
66 {
67 $code = $this->getCode();
68
69 $isRedirect = in_array(
70 $code, [300, 301, 302, 303, 307], true
71 ) || ($code > 307 && $code < 400);
72
73 return $isRedirect || $this->getHttpResponseObject()
74 ->get_response_object()
75 ->redirects > 0;
76 }
77
78 /**
79 * Throw an exception based on the response status code.
80 *
81 * @throws \WpOrg\Requests\Exception\Http\Status
82 */
83 public function throw()
84 {
85 $class = sprintf(
86 'WpOrg\Requests\Exception\Http\Status%d', $this->getCode()
87 );
88
89 if (!class_exists($class)) {
90 $class = 'WpOrg\Requests\Exception\Http\Status\Http';
91 }
92
93 throw new $class;
94 }
95
96 /**
97 * Throw an exception if the given callback returns true.
98 *
99 * @param callable $callback
100 * @return void
101 */
102 public function throwIf(callable $callback)
103 {
104 if ($callback($this)) {
105 return $this->throw();
106 }
107 }
108
109 /**
110 * Get the HTTP response status code.
111 *
112 * @return int
113 */
114 public function getCode()
115 {
116 return wp_remote_retrieve_response_code($this->response);
117 }
118
119 /**
120 * Get the HTTP response status code (alias for getCode).
121 *
122 * @return int
123 */
124 public function getStatus()
125 {
126 return $this->getCode();
127 }
128
129 /**
130 * Get the HTTP response status message.
131 *
132 * @return string
133 */
134 public function getMessage()
135 {
136 return wp_remote_retrieve_response_message($this->response);
137 }
138
139 /**
140 * Get the HTTP response body.
141 *
142 * @return string
143 */
144 public function getBody()
145 {
146 return wp_remote_retrieve_body($this->response);
147 }
148
149 /**
150 * Determine if the response body is JSON.
151 *
152 * @return bool
153 */
154 public function isJson()
155 {
156 $header = $this->getHeader('content-type');
157 return str_contains($header, 'application/json');
158 }
159
160 /**
161 * Get the decoded JSON body.
162 *
163 * @return array|null
164 */
165 public function getJson()
166 {
167 if ($this->isJson()) {
168 return json_decode($this->getBody(), true);
169 }
170
171 return null;
172 }
173
174 /**
175 * Get the headers from the response.
176 *
177 * @return array
178 */
179 public function getHeaders()
180 {
181 return wp_remote_retrieve_headers($this->response);
182 }
183
184 /**
185 * Get a specific header from the response.
186 *
187 * @param string $key The header key.
188 * @return string|null
189 */
190 public function getHeader($key)
191 {
192 return wp_remote_retrieve_header($this->response, $key);
193 }
194
195 /**
196 * Get the cookies from the response.
197 *
198 * @return array
199 */
200 public function getCookies()
201 {
202 return wp_remote_retrieve_cookies($this->response);
203 }
204
205 /**
206 * Get a specific cookie from the response.
207 *
208 * @param string $name The cookie name.
209 * @param bool $isObject Whether to return the \WP_Http_Cookie object.
210 * @return mixed
211 */
212 public function getCookie($name, $isObject = false)
213 {
214 if ($isObject) {
215 // Return the \WP_Http_Cookie object
216 return wp_remote_retrieve_cookie($this->response, $name);
217 }
218
219 return wp_remote_retrieve_cookie_value($this->response, $name);
220 }
221 }
222