3 * HTTP API: WP_HTTP_Response class
11 * Core class used to prepare HTTP responses.
15 class WP_HTTP_Response {
50 * @param mixed $data Response data. Default null.
51 * @param int $status Optional. HTTP status code. Default 200.
52 * @param array $headers Optional. HTTP header map. Default empty array.
54 public function __construct( $data = null, $status = 200, $headers = array() ) {
56 $this->set_status( $status );
57 $this->set_headers( $headers );
61 * Retrieves headers associated with the response.
66 * @return array Map of header name to header value.
68 public function get_headers() {
69 return $this->headers;
73 * Sets all header values.
78 * @param array $headers Map of header name to header value.
80 public function set_headers( $headers ) {
81 $this->headers = $headers;
85 * Sets a single HTTP header.
90 * @param string $key Header name.
91 * @param string $value Header value.
92 * @param bool $replace Optional. Whether to replace an existing header of the same name.
95 public function header( $key, $value, $replace = true ) {
96 if ( $replace || ! isset( $this->headers[ $key ] ) ) {
97 $this->headers[ $key ] = $value;
99 $this->headers[ $key ] .= ', ' . $value;
104 * Retrieves the HTTP return code for the response.
109 * @return int The 3-digit HTTP status code.
111 public function get_status() {
112 return $this->status;
116 * Sets the 3-digit HTTP status code.
121 * @param int $code HTTP status.
123 public function set_status( $code ) {
124 $this->status = absint( $code );
128 * Retrieves the response data.
133 * @return mixed Response data.
135 public function get_data() {
140 * Sets the response data.
145 * @param mixed $data Response data.
147 public function set_data( $data ) {
152 * Retrieves the response data for JSON serialization.
154 * It is expected that in most implementations, this will return the same as get_data(),
155 * however this may be different if you want to do custom JSON data handling.
160 * @return mixed Any JSON-serializable value.
162 public function jsonSerialize() {
163 return $this->get_data();