函数文档

wp_remote_retrieve_cookie_value()

💡 云策文档标注

概述

wp_remote_retrieve_cookie_value() 函数用于从 HTTP 响应中按名称检索单个 cookie 的值。它基于 wp_remote_retrieve_cookie() 获取 cookie 对象,并返回其值或空字符串。

关键要点

  • 参数:$response(必需,数组或 WP_Error 类型,HTTP 响应),$name(必需,字符串类型,要检索的 cookie 名称)
  • 返回值:字符串类型,返回 cookie 的值;如果响应中不存在该 cookie,则返回空字符串
  • 内部实现:调用 wp_remote_retrieve_cookie() 获取 WP_Http_Cookie 对象,检查对象类型后返回其 value 属性
  • 引入版本:WordPress 4.4.0

代码示例

function wp_remote_retrieve_cookie_value( $response, $name ) {
    $cookie = wp_remote_retrieve_cookie( $response, $name );

    if ( ! ( $cookie instanceof WP_Http_Cookie ) ) {
        return '';
    }

    return $cookie->value;
}

📄 原文内容

Retrieves a single cookie’s value by name from the raw response.

Parameters

$responsearray|WP_Errorrequired
HTTP response.
$namestringrequired
The name of the cookie to retrieve.

Return

string The value of the cookie, or empty string if the cookie is not present in the response.

Source

function wp_remote_retrieve_cookie_value( $response, $name ) {
	$cookie = wp_remote_retrieve_cookie( $response, $name );

	if ( ! ( $cookie instanceof WP_Http_Cookie ) ) {
		return '';
	}

	return $cookie->value;
}

Changelog

Version Description
4.4.0 Introduced.