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. * @see \Requests_Utility_CaseInsensitiveDictionary 
  43. * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value. 
  44. */ 
  45. public function get_headers() { 
  46. // Ensure headers remain case-insensitive. 
  47. $converted = new Requests_Utility_CaseInsensitiveDictionary(); 
  48.  
  49. foreach ( $this->response->headers->getAll() as $key => $value ) { 
  50. if ( count( $value ) === 1 ) { 
  51. $converted[ $key ] = $value[0]; 
  52. } else { 
  53. $converted[ $key ] = $value; 
  54.  
  55. return $converted; 
  56.  
  57. /** 
  58. * Sets all header values. 
  59. * @since 4.6.0 
  60. * @access public 
  61. * @param array $headers Map of header name to header value. 
  62. */ 
  63. public function set_headers( $headers ) { 
  64. $this->response->headers = new Requests_Response_Headers( $headers ); 
  65.  
  66. /** 
  67. * Sets a single HTTP header. 
  68. * @since 4.6.0 
  69. * @access public 
  70. * @param string $key Header name. 
  71. * @param string $value Header value. 
  72. * @param bool $replace Optional. Whether to replace an existing header of the same name. 
  73. * Default true. 
  74. */ 
  75. public function header( $key, $value, $replace = true ) { 
  76. if ( $replace ) { 
  77. unset( $this->response->headers[ $key ] ); 
  78.  
  79. $this->response->headers[ $key ] = $value; 
  80.  
  81. /** 
  82. * Retrieves the HTTP return code for the response. 
  83. * @since 4.6.0 
  84. * @access public 
  85. * @return int The 3-digit HTTP status code. 
  86. */ 
  87. public function get_status() { 
  88. return $this->response->status_code; 
  89.  
  90. /** 
  91. * Sets the 3-digit HTTP status code. 
  92. * @since 4.6.0 
  93. * @access public 
  94. * @param int $code HTTP status. 
  95. */ 
  96. public function set_status( $code ) { 
  97. $this->response->status_code = absint( $code ); 
  98.  
  99. /** 
  100. * Retrieves the response data. 
  101. * @since 4.6.0 
  102. * @access public 
  103. * @return mixed Response data. 
  104. */ 
  105. public function get_data() { 
  106. return $this->response->body; 
  107.  
  108. /** 
  109. * Sets the response data. 
  110. * @since 4.6.0 
  111. * @access public 
  112. * @param mixed $data Response data. 
  113. */ 
  114. public function set_data( $data ) { 
  115. $this->response->body = $data; 
  116.  
  117. /** 
  118. * Retrieves cookies from the response. 
  119. * @since 4.6.0 
  120. * @access public 
  121. * @return WP_HTTP_Cookie[] List of cookie objects. 
  122. */ 
  123. public function get_cookies() { 
  124. $cookies = array(); 
  125. foreach ( $this->response->cookies as $cookie ) { 
  126. $cookies[] = new WP_Http_Cookie( array( 
  127. 'name' => $cookie->name,  
  128. 'value' => urldecode( $cookie->value ),  
  129. 'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,  
  130. 'path' => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,  
  131. 'domain' => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,  
  132. )); 
  133.  
  134. return $cookies; 
  135.  
  136. /** 
  137. * Converts the object to a WP_Http response array. 
  138. * @since 4.6.0 
  139. * @access public 
  140. * @return array WP_Http response array, per WP_Http::request(). 
  141. */ 
  142. public function to_array() { 
  143. return array( 
  144. 'headers' => $this->get_headers(),  
  145. 'body' => $this->get_data(),  
  146. 'response' => array( 
  147. 'code' => $this->get_status(),  
  148. 'message' => get_status_header_desc( $this->get_status() ),  
  149. ),  
  150. 'cookies' => $this->get_cookies(),  
  151. 'filename' => $this->filename,  
  152. );