本文档介绍了在WordPress中进行API身份验证的方法,特别是HTTP Basic Authentication的使用。它强调了这种方法的局限性,并提供了相关资源链接。
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( YOUR_USERNAME . ':' . YOUR_PASSWORD )
)
);
wp_remote_get( $url, $args );HTTP Basic Authentication仅用于测试和开发,生产环境中应使用更安全的身份验证方法。
Many APIs will require you to make authenticated requests to access some endpoints. A common authentication method is called HTTP Basic Authentication. It can be used in WordPress using the ‘Authorization’ header <a href="https://developer.wordpress.org/reference/functions/wp_remote_get/">wp_remote_get()</a>.
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( YOUR_USERNAME . ':' . YOUR_PASSWORD )
)
);
wp_remote_get( $url, $args );
HTTP Basic Auth is very insecure because it exposes the username and password and is only used for testing and development. Check the documentation of the API you want to access for more information on how to authenticate.
If you want to make authenticated requests to the WordPress REST API, check this article.