WP_HTTP_Response

Core class used to prepare HTTP responses.

Defined (1)

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

/wp-includes/class-wp-http-response.php  
  1. class WP_HTTP_Response { 
  2.  
  3. /** 
  4. * Response data. 
  5. * @since 4.4.0 
  6. * @access public 
  7. * @var mixed 
  8. */ 
  9. public $data; 
  10.  
  11. /** 
  12. * Response headers. 
  13. * @since 4.4.0 
  14. * @access public 
  15. * @var int 
  16. */ 
  17. public $headers; 
  18.  
  19. /** 
  20. * Response status. 
  21. * @since 4.4.0 
  22. * @access public 
  23. * @var array 
  24. */ 
  25. public $status; 
  26.  
  27. /** 
  28. * Constructor. 
  29. * @since 4.4.0 
  30. * @access public 
  31. * @param mixed $data Response data. Default null. 
  32. * @param int $status Optional. HTTP status code. Default 200. 
  33. * @param array $headers Optional. HTTP header map. Default empty array. 
  34. */ 
  35. public function __construct( $data = null, $status = 200, $headers = array() ) { 
  36. $this->data = $data; 
  37. $this->set_status( $status ); 
  38. $this->set_headers( $headers ); 
  39.  
  40. /** 
  41. * Retrieves headers associated with the response. 
  42. * @since 4.4.0 
  43. * @access public 
  44. * @return array Map of header name to header value. 
  45. */ 
  46. public function get_headers() { 
  47. return $this->headers; 
  48.  
  49. /** 
  50. * Sets all header values. 
  51. * @since 4.4.0 
  52. * @access public 
  53. * @param array $headers Map of header name to header value. 
  54. */ 
  55. public function set_headers( $headers ) { 
  56. $this->headers = $headers; 
  57.  
  58. /** 
  59. * Sets a single HTTP header. 
  60. * @since 4.4.0 
  61. * @access public 
  62. * @param string $key Header name. 
  63. * @param string $value Header value. 
  64. * @param bool $replace Optional. Whether to replace an existing header of the same name. 
  65. * Default true. 
  66. */ 
  67. public function header( $key, $value, $replace = true ) { 
  68. if ( $replace || ! isset( $this->headers[ $key ] ) ) { 
  69. $this->headers[ $key ] = $value; 
  70. } else { 
  71. $this->headers[ $key ] .= ', ' . $value; 
  72.  
  73. /** 
  74. * Retrieves the HTTP return code for the response. 
  75. * @since 4.4.0 
  76. * @access public 
  77. * @return int The 3-digit HTTP status code. 
  78. */ 
  79. public function get_status() { 
  80. return $this->status; 
  81.  
  82. /** 
  83. * Sets the 3-digit HTTP status code. 
  84. * @since 4.4.0 
  85. * @access public 
  86. * @param int $code HTTP status. 
  87. */ 
  88. public function set_status( $code ) { 
  89. $this->status = absint( $code ); 
  90.  
  91. /** 
  92. * Retrieves the response data. 
  93. * @since 4.4.0 
  94. * @access public 
  95. * @return mixed Response data. 
  96. */ 
  97. public function get_data() { 
  98. return $this->data; 
  99.  
  100. /** 
  101. * Sets the response data. 
  102. * @since 4.4.0 
  103. * @access public 
  104. * @param mixed $data Response data. 
  105. */ 
  106. public function set_data( $data ) { 
  107. $this->data = $data; 
  108.  
  109. /** 
  110. * Retrieves the response data for JSON serialization. 
  111. * It is expected that in most implementations, this will return the same as get_data(),  
  112. * however this may be different if you want to do custom JSON data handling. 
  113. * @since 4.4.0 
  114. * @access public 
  115. * @return mixed Any JSON-serializable value. 
  116. */ 
  117. public function jsonSerialize() { 
  118. return $this->get_data();