函數(shù)功能
使用POST方法執(zhí)行HTTP請(qǐng)求并返回其響應(yīng)數(shù)據(jù)。
參數(shù)
| 參數(shù) | 數(shù)據(jù)類型 | 是否必需 | 描述 | 默認(rèn)值 |
|---|---|---|---|---|
| $url | 字符串 | 是 | 請(qǐng)求 URL | 無(wú) |
| $args | 數(shù)組 | 否 | 請(qǐng)求參數(shù) | array() |
返回值
響應(yīng)數(shù)組,如果出錯(cuò),返回 WP_Error 對(duì)象
使用示例
發(fā)送的Post數(shù)據(jù)應(yīng)該在 body 中提供,body 不一定是數(shù)組,也可以是 XML 或 JSON 格式的字符串或其他可以通過(guò) HTTP 協(xié)議發(fā)送的數(shù)據(jù)。
$response = wp_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>';
}
在請(qǐng)求中添加基礎(chǔ)授權(quán)數(shù)據(jù)
如果需要添加基礎(chǔ)授權(quán)數(shù)據(jù),參考下面的代碼在 header 中添加即可。
$response = wp_remote_post( $url, array(
'body' => $data,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
) );
相關(guān)函數(shù)
wp_safe_remote_post: wp_remote_post 的帶安全驗(yàn)證的版本
wp_remote_get: 用 GET 方法從 URL 中獲取數(shù)據(jù)