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 * @see \Requests_Utility_CaseInsensitiveDictionary
70 * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value.
72 public function get_headers() {
73 // Ensure headers remain case-insensitive.
74 $converted = new Requests_Utility_CaseInsensitiveDictionary();
76 foreach ( $this->response->headers->getAll() as $key => $value ) {
77 if ( count( $value ) === 1 ) {
78 $converted[ $key ] = $value[0];
80 $converted[ $key ] = $value;
88 * Sets all header values.
93 * @param array $headers Map of header name to header value.
95 public function set_headers( $headers ) {
96 $this->response->headers = new Requests_Response_Headers( $headers );
100 * Sets a single HTTP header.
105 * @param string $key Header name.
106 * @param string $value Header value.
107 * @param bool $replace Optional. Whether to replace an existing header of the same name.
110 public function header( $key, $value, $replace = true ) {
112 unset( $this->response->headers[ $key ] );
115 $this->response->headers[ $key ] = $value;
119 * Retrieves the HTTP return code for the response.
124 * @return int The 3-digit HTTP status code.
126 public function get_status() {
127 return $this->response->status_code;
131 * Sets the 3-digit HTTP status code.
136 * @param int $code HTTP status.
138 public function set_status( $code ) {
139 $this->response->status_code = absint( $code );
143 * Retrieves the response data.
148 * @return mixed Response data.
150 public function get_data() {
151 return $this->response->body;
155 * Sets the response data.
160 * @param mixed $data Response data.
162 public function set_data( $data ) {
163 $this->response->body = $data;
167 * Retrieves cookies from the response.
172 * @return WP_HTTP_Cookie[] List of cookie objects.
174 public function get_cookies() {
176 foreach ( $this->response->cookies as $cookie ) {
177 $cookies[] = new WP_Http_Cookie( array(
178 'name' => $cookie->name,
179 'value' => urldecode( $cookie->value ),
180 'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
181 'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
182 'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
190 * Converts the object to a WP_Http response array.
195 * @return array WP_Http response array, per WP_Http::request().
197 public function to_array() {
199 'headers' => $this->get_headers(),
200 'body' => $this->get_data(),
202 'code' => $this->get_status(),
203 'message' => get_status_header_desc( $this->get_status() ),
205 'cookies' => $this->get_cookies(),
206 'filename' => $this->filename,