函数文档

wp_remote_head()

💡 云策文档标注

概述

wp_remote_head() 函数用于执行 HTTP HEAD 请求并返回响应。它基于 WP_Http 类实现,适用于 WordPress 开发者进行远程 HTTP 操作。

关键要点

  • 执行 HTTP HEAD 方法请求,返回响应数组或 WP_Error 对象。
  • 如果 URL 由用户控制,建议使用 wp_safe_remote_head() 以提高安全性。
  • 参数包括必需的 $url 字符串和可选的 $args 数组,后者可参考 WP_Http::request() 设置。
  • 函数内部调用 _wp_http_get_object() 获取 WP_Http 对象并执行 head() 方法。
  • 自 WordPress 2.7.0 版本引入,相关函数包括 wp_remote_request() 和 WP_Http::request()。

代码示例

function wp_remote_head( $url, $args = array() ) {
	$http = _wp_http_get_object();
	return $http->head( $url, $args );
}

注意事项

  • 注意安全性:当 URL 来自用户输入时,优先使用 wp_safe_remote_head() 来防止潜在的安全风险。
  • 响应格式和默认参数可参考 WP_Http::request() 文档。

📄 原文内容

Performs an HTTP request using the HEAD method and returns its response.

Description

Important: If the URL is user-controlled, use wp_safe_remote_head() instead.

See also

Parameters

$urlstringrequired
URL to retrieve.
$argsarrayoptional
Request arguments.
See WP_Http::request() for information on accepted arguments.

Default:array()

Return

array|WP_Error The response or WP_Error on failure.
See WP_Http::request() for information on return value.

Source

function wp_remote_head( $url, $args = array() ) {
	$http = _wp_http_get_object();
	return $http->head( $url, $args );
}

Changelog

Version Description
2.7.0 Introduced.