3 * HTTP API: WP_HTTP_Requests_Response class
11 * Core wrapper object for a Requests_Response for standardisation.
15 * @see WP_HTTP_Response
17 class WP_HTTP_Requests_Response extends WP_HTTP_Response {
19 * Requests Response object.
23 * @var Requests_Response
28 * Filename the response was saved to.
42 * @param Requests_Response $response HTTP response.
43 * @param string $filename Optional. File name. Default empty.
45 public function __construct( Requests_Response $response, $filename = '' ) {
46 $this->response = $response;
47 $this->filename = $filename;
51 * Retrieves the response object for the request.
56 * @return Requests_Response HTTP response.
58 public function get_response_object() {
59 return $this->response;
63 * Retrieves headers associated with the response.
68 * @return array Map of header name to header value.
70 public function get_headers() {
71 // Ensure headers remain case-insensitive
72 $converted = new Requests_Utility_CaseInsensitiveDictionary();
74 foreach ( $this->response->headers->getAll() as $key => $value ) {
75 if ( count( $value ) === 1 ) {
76 $converted[ $key ] = $value[0];
79 $converted[ $key ] = $value;
87 * Sets all header values.
92 * @param array $headers Map of header name to header value.
94 public function set_headers( $headers ) {
95 $this->response->headers = new Requests_Response_Headers( $headers );
99 * Sets a single HTTP header.
104 * @param string $key Header name.
105 * @param string $value Header value.
106 * @param bool $replace Optional. Whether to replace an existing header of the same name.
109 public function header( $key, $value, $replace = true ) {
111 unset( $this->response->headers[ $key ] );
114 $this->response->headers[ $key ] = $value;
118 * Retrieves the HTTP return code for the response.
123 * @return int The 3-digit HTTP status code.
125 public function get_status() {
126 return $this->response->status_code;
130 * Sets the 3-digit HTTP status code.
135 * @param int $code HTTP status.
137 public function set_status( $code ) {
138 $this->response->status_code = absint( $code );
142 * Retrieves the response data.
147 * @return mixed Response data.
149 public function get_data() {
150 return $this->response->body;
154 * Sets the response data.
159 * @param mixed $data Response data.
161 public function set_data( $data ) {
162 $this->response->body = $data;
166 * Retrieves cookies from the response.
171 * @return WP_HTTP_Cookie[] List of cookie objects.
173 public function get_cookies() {
175 foreach ( $this->response->cookies as $cookie ) {
176 $cookies[] = new WP_Http_Cookie( array(
177 'name' => $cookie->name,
178 'value' => urldecode( $cookie->value ),
179 'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
180 'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
181 'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
189 * Converts the object to a WP_Http response array.
194 * @return array WP_Http response array, per WP_Http::request().
196 public function to_array() {
198 'headers' => $this->get_headers(),
199 'body' => $this->get_data(),
201 'code' => $this->get_status(),
202 'message' => get_status_header_desc( $this->get_status() ),
204 'cookies' => $this->get_cookies(),
205 'filename' => $this->filename,