函数文档

wp_safe_remote_post()

💡 云策文档标注

概述

wp_safe_remote_post() 函数用于通过 POST 方法执行安全的 HTTP 请求并获取原始响应。它通过 URL 验证防止 SSRF 攻击,仅支持 http 和 https 协议。

关键要点

  • 函数用于向任意 URL 发起安全的 POST 请求,自动验证 URL 和重定向 URL 以避免 SSRF 攻击。
  • 参数包括必需的 $url(字符串)和可选的 $args(数组),后者参考 WP_Http::request() 的默认参数。
  • 返回值为响应数组或 WP_Error 对象,失败时返回 WP_Error。
  • 内部实现通过设置 reject_unsafe_urls 为 true 并调用 WP_Http 对象的 post 方法。
  • 相关函数包括 wp_remote_request()、WP_Http::request() 和 wp_http_validate_url(),用于扩展功能或参考。

代码示例

function wp_safe_remote_post( $url, $args = array() ) {
    $args['reject_unsafe_urls'] = true;
    $http                       = _wp_http_get_object();
    return $http->post( $url, $args );
}

注意事项

  • 仅支持 http 和 https 协议,确保 URL 安全验证。
  • 使用前应参考 WP_Http::request() 了解参数和返回值细节。
  • 函数自 WordPress 3.6.0 版本引入,适用于需要安全远程 POST 请求的场景。

📄 原文内容

Retrieves the raw response from a safe HTTP request using the POST 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

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_safe_remote_post( $url, $args = array() ) {
	$args['reject_unsafe_urls'] = true;
	$http                       = _wp_http_get_object();
	return $http->post( $url, $args );
}

Changelog

Version Description
3.6.0 Introduced.

User Contributed Notes