Performs an HTTP request using the POST method and returns its response.
Description
See also
- wp_remote_request(): For more information on the response array format.
- WP_Http::request(): For default arguments information.
Parameters
$url
stringrequired- URL to retrieve.
$args
arrayoptional- Request arguments.
See WP_Http::request() for information on accepted arguments.Default:
array()
Source
function wp_remote_post( $url, $args = array() ) {
$http = _wp_http_get_object();
return $http->post( $url, $args );
}
Changelog
Version | Description |
---|---|
2.7.0 | Introduced. |
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: