A couple notes I thought worth mentioning is that if you’re using a WordPress API endpoint that requires authentication, you can pass the _wpnonce parameter along with the session cookies to properly authenticate, like so:
Note that I’m also using wp_remote_post to perform a GET call in this example (by passing the ‘method’ argument), otherwise I could use the wp_remote_get function instead
Exemple of Basic Authentication:
Post data should be sent in the body as an array. Example passing post data:
In the example above,
$response['body']
will contain the actual page content returned by the server.An example of a JSON body:
See also wp_safe_remote_post(), especially if you are using a dynamic URL call.
A couple notes I thought worth mentioning is that if you’re using a WordPress API endpoint that requires authentication, you can pass the
_wpnonce
parameter along with the session cookies to properly authenticate, like so:Note that I’m also using
wp_remote_post
to perform aGET
call in this example (by passing the ‘method’ argument), otherwise I could use the wp_remote_get function insteadHandle errors using is_wp_error() as follows: