wp_safe_remote_request()
云策文档标注
概述
wp_safe_remote_request() 函数用于执行安全的 HTTP 请求并返回原始响应,适用于向任意 URL 发起请求的场景。它通过 wp_http_validate_url() 验证 URL 及其重定向,以防止 SSRF 攻击,仅支持 http 和 https 协议。
关键要点
- 函数用途:执行安全的 HTTP 请求,返回响应数组或 WP_Error 对象。
- 安全机制:自动设置 reject_unsafe_urls 参数为 true,利用 wp_http_validate_url() 验证 URL,防范 SSRF 攻击。
- 参数说明:$url 为必需参数,指定请求的 URL;$args 为可选参数,传递请求参数,默认值为空数组。
- 相关函数:可参考 wp_remote_request() 了解响应格式,WP_Http::request() 获取默认参数信息,wp_http_validate_url() 了解 URL 验证细节。
- 源代码:函数内部调用 _wp_http_get_object() 获取 WP_Http 对象,并执行 request 方法。
- 版本历史:自 WordPress 3.6.0 版本引入。
代码示例
function wp_safe_remote_request( $url, $args = array() ) {
$args['reject_unsafe_urls'] = true;
$http = _wp_http_get_object();
return $http->request( $url, $args );
}
原文内容
Retrieves the raw response from a safe HTTP request.
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_request( $url, $args = array() ) {
$args['reject_unsafe_urls'] = true;
$http = _wp_http_get_object();
return $http->request( $url, $args );
}
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |