WP_HTTP_Requests_Response

Core wrapper object for a Requests_Response for standardisation.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-http-requests-response.php  
  1. class WP_HTTP_Requests_Response extends WP_HTTP_Response { 
  2. /** 
  3. * Requests Response object. 
  4. * @since 4.6.0 
  5. * @access protected 
  6. * @var Requests_Response 
  7. */ 
  8. protected $response; 
  9.  
  10. /** 
  11. * Filename the response was saved to. 
  12. * @since 4.6.0 
  13. * @access protected 
  14. * @var string|null 
  15. */ 
  16. protected $filename; 
  17.  
  18. /** 
  19. * Constructor. 
  20. * @since 4.6.0 
  21. * @access public 
  22. * @param Requests_Response $response HTTP response. 
  23. * @param string $filename Optional. File name. Default empty. 
  24. */ 
  25. public function __construct( Requests_Response $response, $filename = '' ) { 
  26. $this->response = $response; 
  27. $this->filename = $filename; 
  28.  
  29. /** 
  30. * Retrieves the response object for the request. 
  31. * @since 4.6.0 
  32. * @access public 
  33. * @return Requests_Response HTTP response. 
  34. */ 
  35. public function get_response_object() { 
  36. return $this->response; 
  37.  
  38. /** 
  39. * Retrieves headers associated with the response. 
  40. * @since 4.6.0 
  41. * @access public 
  42. * @return array Map of header name to header value. 
  43. */ 
  44. public function get_headers() { 
  45. // Ensure headers remain case-insensitive 
  46. $converted = new Requests_Utility_CaseInsensitiveDictionary(); 
  47.  
  48. foreach ( $this->response->headers->getAll() as $key => $value ) { 
  49. if ( count( $value ) === 1 ) { 
  50. $converted[ $key ] = $value[0]; 
  51. else { 
  52. $converted[ $key ] = $value; 
  53.  
  54. return $converted; 
  55.  
  56. /** 
  57. * Sets all header values. 
  58. * @since 4.6.0 
  59. * @access public 
  60. * @param array $headers Map of header name to header value. 
  61. */ 
  62. public function set_headers( $headers ) { 
  63. $this->response->headers = new Requests_Response_Headers( $headers ); 
  64.  
  65. /** 
  66. * Sets a single HTTP header. 
  67. * @since 4.6.0 
  68. * @access public 
  69. * @param string $key Header name. 
  70. * @param string $value Header value. 
  71. * @param bool $replace Optional. Whether to replace an existing header of the same name. 
  72. * Default true. 
  73. */ 
  74. public function header( $key, $value, $replace = true ) { 
  75. if ( $replace ) { 
  76. unset( $this->response->headers[ $key ] ); 
  77.  
  78. $this->response->headers[ $key ] = $value; 
  79.  
  80. /** 
  81. * Retrieves the HTTP return code for the response. 
  82. * @since 4.6.0 
  83. * @access public 
  84. * @return int The 3-digit HTTP status code. 
  85. */ 
  86. public function get_status() { 
  87. return $this->response->status_code; 
  88.  
  89. /** 
  90. * Sets the 3-digit HTTP status code. 
  91. * @since 4.6.0 
  92. * @access public 
  93. * @param int $code HTTP status. 
  94. */ 
  95. public function set_status( $code ) { 
  96. $this->response->status_code = absint( $code ); 
  97.  
  98. /** 
  99. * Retrieves the response data. 
  100. * @since 4.6.0 
  101. * @access public 
  102. * @return mixed Response data. 
  103. */ 
  104. public function get_data() { 
  105. return $this->response->body; 
  106.  
  107. /** 
  108. * Sets the response data. 
  109. * @since 4.6.0 
  110. * @access public 
  111. * @param mixed $data Response data. 
  112. */ 
  113. public function set_data( $data ) { 
  114. $this->response->body = $data; 
  115.  
  116. /** 
  117. * Retrieves cookies from the response. 
  118. * @since 4.6.0 
  119. * @access public 
  120. * @return WP_HTTP_Cookie[] List of cookie objects. 
  121. */ 
  122. public function get_cookies() { 
  123. $cookies = array(); 
  124. foreach ( $this->response->cookies as $cookie ) { 
  125. $cookies[] = new WP_Http_Cookie( array( 
  126. 'name' => $cookie->name,  
  127. 'value' => urldecode( $cookie->value ),  
  128. 'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,  
  129. 'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,  
  130. 'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,  
  131. )); 
  132.  
  133. return $cookies; 
  134.  
  135. /** 
  136. * Converts the object to a WP_Http response array. 
  137. * @since 4.6.0 
  138. * @access public 
  139. * @return array WP_Http response array, per WP_Http::request(). 
  140. */ 
  141. public function to_array() { 
  142. return array( 
  143. 'headers' => $this->get_headers(),  
  144. 'body' => $this->get_data(),  
  145. 'response' => array( 
  146. 'code' => $this->get_status(),  
  147. 'message' => get_status_header_desc( $this->get_status() ),  
  148. ),  
  149. 'cookies' => $this->get_cookies(),  
  150. 'filename' => $this->filename,  
  151. );