wp_safe_remote_post uses the POST method to get the raw response from a safe HTTP request

Posted in:
Update time:2024-08-06

向任意 URL 发出 HTTP 请求时,此函数非常理想。 URL 经过验证以避免重定向和请求伪造攻击。

function parameter

parametersdata typeRequired or notdescriptivedefault value
$urlstring (computer science)beRequest URLnot have
$argsarraysclogged请求参数array()

Function Return Value

响应数组,如果出错,返回 WP_Error 对象

usage example

发送的Post数据应该在 body 中提供,body 不一定是数组,也可以是 XML 或 JSON 格式的字符串或其他可以通过 HTTP 协议发送的数据。

$response = wp_safe_remote_post( $url, array(
    'timeout'     => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking'    => true,
    'headers'     => array(),
    'body'        => array(
        'username' => 'bob',
        'password' => '1234xyz'
    ),
    'cookies'     => array()
    )
);
 
if ( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    echo "Something went wrong: $error_message";
} else {
    echo 'Response:<pre>';
    print_r( $response );
    echo '</pre>';
}

related function

wp_remote_post: 发送 HTTP POST 请求并获取返回数据

wp_remote_get: 用 GET 方法从 URL 中获取数据

We offer WordPress Themes and Plugins Custom Development Services

This site has long undertaken WordPress themes, plugins, WooCommerce-based store mall development business. We have 10 years of experience in WordPress development, if you want to Developing Websites with WordPress, please contact WeChat: iwillhappy1314 or email: amos@wpcio.com for inquiries.

发表回复

Your email address will not be published. 必填项已用 * 标注

*