类文档

WP_HTTP_Response

💡 云策文档标注

概述

WP_HTTP_Response 是 WordPress 核心类,用于准备 HTTP 响应,包含数据、状态码和头信息的管理。该类提供了一系列方法来设置和获取响应属性,支持 JSON 序列化。

关键要点

  • WP_HTTP_Response 类用于构建 HTTP 响应,包含 $data、$headers 和 $status 三个公共属性。
  • 提供构造函数 __construct() 初始化响应,以及 get_data()、set_data()、get_headers()、set_headers()、get_status()、set_status() 等方法操作属性。
  • header() 方法可设置单个 HTTP 头,支持替换或追加;jsonSerialize() 方法用于 JSON 序列化,默认返回 get_data() 的数据。
  • 自 WordPress 4.4.0 引入,常用于 WP_HTTP_Requests_Response 和 WP_REST_Response 等核心组件。

代码示例

// 创建 WP_HTTP_Response 实例
$response = new WP_HTTP_Response( 'Hello World', 200, array( 'Content-Type' => 'text/plain' ) );

// 获取和设置数据
$data = $response->get_data();
$response->set_data( array( 'message' => 'Updated' ) );

// 操作头信息
$headers = $response->get_headers();
$response->header( 'X-Custom-Header', 'Value', true );

// 获取和设置状态码
$status = $response->get_status();
$response->set_status( 404 );

// JSON 序列化
$json_data = $response->jsonSerialize();

注意事项

  • jsonSerialize() 方法名称不符合 WordPress 命名规范,但为兼容 PHP 接口保留。
  • set_status() 方法使用 absint() 确保状态码为整数,避免无效输入。
  • header() 方法的 $replace 参数默认为 true,表示替换同名头;设为 false 可追加值。

📄 原文内容

Core class used to prepare HTTP responses.

Methods

Name Description
WP_HTTP_Response::__construct Constructor.
WP_HTTP_Response::get_data Retrieves the response data.
WP_HTTP_Response::get_headers Retrieves headers associated with the response.
WP_HTTP_Response::get_status Retrieves the HTTP return code for the response.
WP_HTTP_Response::header Sets a single HTTP header.
WP_HTTP_Response::jsonSerialize Retrieves the response data for JSON serialization.
WP_HTTP_Response::set_data Sets the response data.
WP_HTTP_Response::set_headers Sets all header values.
WP_HTTP_Response::set_status Sets the 3-digit HTTP status code.

Source

class WP_HTTP_Response {

	/**
	 * Response data.
	 *
	 * @since 4.4.0
	 * @var mixed
	 */
	public $data;

	/**
	 * Response headers.
	 *
	 * @since 4.4.0
	 * @var array
	 */
	public $headers;

	/**
	 * Response status.
	 *
	 * @since 4.4.0
	 * @var int
	 */
	public $status;

	/**
	 * Constructor.
	 *
	 * @since 4.4.0
	 *
	 * @param mixed $data    Response data. Default null.
	 * @param int   $status  Optional. HTTP status code. Default 200.
	 * @param array $headers Optional. HTTP header map. Default empty array.
	 */
	public function __construct( $data = null, $status = 200, $headers = array() ) {
		$this->set_data( $data );
		$this->set_status( $status );
		$this->set_headers( $headers );
	}

	/**
	 * Retrieves headers associated with the response.
	 *
	 * @since 4.4.0
	 *
	 * @return array Map of header name to header value.
	 */
	public function get_headers() {
		return $this->headers;
	}

	/**
	 * Sets all header values.
	 *
	 * @since 4.4.0
	 *
	 * @param array $headers Map of header name to header value.
	 */
	public function set_headers( $headers ) {
		$this->headers = $headers;
	}

	/**
	 * Sets a single HTTP header.
	 *
	 * @since 4.4.0
	 *
	 * @param string $key     Header name.
	 * @param string $value   Header value.
	 * @param bool   $replace Optional. Whether to replace an existing header of the same name.
	 *                        Default true.
	 */
	public function header( $key, $value, $replace = true ) {
		if ( $replace || ! isset( $this->headers[ $key ] ) ) {
			$this->headers[ $key ] = $value;
		} else {
			$this->headers[ $key ] .= ', ' . $value;
		}
	}

	/**
	 * Retrieves the HTTP return code for the response.
	 *
	 * @since 4.4.0
	 *
	 * @return int The 3-digit HTTP status code.
	 */
	public function get_status() {
		return $this->status;
	}

	/**
	 * Sets the 3-digit HTTP status code.
	 *
	 * @since 4.4.0
	 *
	 * @param int $code HTTP status.
	 */
	public function set_status( $code ) {
		$this->status = absint( $code );
	}

	/**
	 * Retrieves the response data.
	 *
	 * @since 4.4.0
	 *
	 * @return mixed Response data.
	 */
	public function get_data() {
		return $this->data;
	}

	/**
	 * Sets the response data.
	 *
	 * @since 4.4.0
	 *
	 * @param mixed $data Response data.
	 */
	public function set_data( $data ) {
		$this->data = $data;
	}

	/**
	 * Retrieves the response data for JSON serialization.
	 *
	 * It is expected that in most implementations, this will return the same as get_data(),
	 * however this may be different if you want to do custom JSON data handling.
	 *
	 * @since 4.4.0
	 *
	 * @return mixed Any JSON-serializable value.
	 */
	public function jsonSerialize() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
		return $this->get_data();
	}
}

Changelog

Version Description
4.4.0 Introduced.