wp_safe_remote_head()
云策文档标注
概述
wp_safe_remote_head() 函数用于通过安全的 HTTP HEAD 方法获取原始响应,适用于向任意 URL 发起请求的场景。该函数通过 wp_http_validate_url() 验证 URL 及其重定向,以防止服务器端请求伪造(SSRF)攻击,仅支持 http 和 https 协议。
关键要点
- 函数用途:执行安全的 HTTP HEAD 请求,返回响应数组或 WP_Error。
- 安全特性:自动设置 reject_unsafe_urls 参数为 true,并验证 URL 以避免 SSRF 攻击。
- 参数说明:接受 $url(必需)和 $args(可选,默认为空数组)参数,后者参考 WP_Http::request()。
- 返回值:响应数组或 WP_Error 对象,具体格式参考 WP_Http::request()。
- 相关函数:可参考 wp_remote_request()、WP_Http::request() 和 wp_http_validate_url() 了解更多细节。
- 引入版本:自 WordPress 3.6.0 起可用。
代码示例
function wp_safe_remote_head( $url, $args = array() ) {
$args['reject_unsafe_urls'] = true;
$http = _wp_http_get_object();
return $http->head( $url, $args );
}注意事项
- 仅支持 http 和 https 协议,确保 URL 验证安全。
- 内部使用 _wp_http_get_object() 获取 WP_Http 对象,并调用其 head 方法。
- 相关用途包括 wp_get_http_headers() 和 discover_pingback_server_uri() 函数。
原文内容
Retrieves the raw response from a safe HTTP request using the HEAD method.
Description
This function is ideal when the HTTP request is being made to an arbitrary URL. The URL, and every URL it redirects to, are validated with wp_http_validate_url() to avoid Server Side Request Forgery attacks (SSRF).
The only supported protocols are http and https.
See also
- wp_remote_request(): For more information on the response array format.
- WP_Http::request(): For default arguments information.
- wp_http_validate_url(): For more information about how the URL is validated.
Parameters
$urlstringrequired-
URL to retrieve.
$argsarrayoptional-
Request arguments.
See WP_Http::request() for information on accepted arguments.Default:
array()
Source
function wp_safe_remote_head( $url, $args = array() ) {
$args['reject_unsafe_urls'] = true;
$http = _wp_http_get_object();
return $http->head( $url, $args );
}
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |