wp_remote_post( string $url, array $args = array() ): array|WP_Error

Performs an HTTP request using the POST method and returns its response.


Description

Top ↑

See also


Top ↑

Parameters

$url string Required
URL to retrieve.
$args array Optional
Request arguments.
See WP_Http::request() for information on accepted arguments.
More Arguments from WP_Http::request( ... $args ) Array or string of HTTP request arguments.
  • method string
    Request method. Accepts 'GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', or 'PATCH'.
    Some transports technically allow others, but should not be assumed. Default 'GET'.
  • timeout float
    How long the connection should stay open in seconds. Default 5.
  • redirection int
    Number of allowed redirects. Not supported by all transports.
    Default 5.
  • httpversion string
    Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.
    Default '1.0'.
  • user-agent string
    User-agent value sent.
    Default 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ).
  • reject_unsafe_urls bool
    Whether to pass URLs through wp_http_validate_url() .
    Default false.
  • blocking bool
    Whether the calling code requires the result of the request.
    If set to false, the request will be sent to the remote server, and processing returned to the calling code immediately, the caller will know if the request succeeded or failed, but will not receive any response from the remote server. Default true.
  • headers string|array
    Array or string of headers to send with the request.
  • cookies array
    List of cookies to send with the request.
  • body string|array
    Body to send with the request. Default null.
  • compress bool
    Whether to compress the $body when sending the request.
    Default false.
  • decompress bool
    Whether to decompress a compressed response. If set to false and compressed content is returned in the response anyway, it will need to be separately decompressed. Default true.
  • sslverify bool
    Whether to verify SSL for the request. Default true.
  • sslcertificates string
    Absolute path to an SSL certificate .crt file.
    Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
  • stream bool
    Whether to stream to a file. If set to true and no filename was given, it will be dropped it in the WP temp dir and its name will be set using the basename of the URL. Default false.
  • filename string
    Filename of the file to write to when streaming. $stream must be set to true. Default null.
  • limit_response_size int
    Size in bytes to limit the response to. Default null.

Default: array()


Top ↑

Return

array|WP_Error The response or WP_Error on failure.


Top ↑

Source

File: wp-includes/http.php. View all references

function wp_remote_post( $url, $args = array() ) {
	$http = _wp_http_get_object();
	return $http->post( $url, $args );
}


Top ↑

Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 2 content
    Contributed by Codex

    Post data should be sent in the body as an array. Example passing post data:

    $response = wp_remote_post( $url, array(
    	'method'      => 'POST',
    	'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>';
    }

    In the example above, $response['body'] will contain the actual page content returned by the server.

  2. Skip to note 3 content
    Contributed by Pixelbart

    An example of a JSON body:

    $endpoint = 'api.example.com';
    
    $body = [
    	'name'  => 'Pixelbart',
    	'email' => 'pixelbart@example.com',
    ];
    
    $body = wp_json_encode( $body );
    
    $options = [
    	'body'        => $body,
    	'headers'     => [
    		'Content-Type' => 'application/json',
    	],
    	'timeout'     => 60,
    	'redirection' => 5,
    	'blocking'    => true,
    	'httpversion' => '1.0',
    	'sslverify'   => false,
    	'data_format' => 'body',
    ];
    
    wp_remote_post( $endpoint, $options );
  3. Skip to note 4 content
    Contributed by Mehedi Foysal
    $apiUrl      = 'https://api.example.com';
    $apiResponse = wp_remote_post( $apiUrl,
    	[
    		'method'    => 'GET',
    		'sslverify' => false,
    		'headers'   => [
    			'content-type' => 'application/json',
    			'user_key'     => 'xxxxxxxxxxxxx',
    		],
    	]
    );
    $apiBody     = json_decode( wp_remote_retrieve_body( $apiResponse ) );
  4. Skip to note 6 content
    Contributed by ouija

    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:

    $endpoint = get_rest_url( null, 'ninja-forms-submissions/submissions/get' );
    $body = array(
    	'_wpnonce' => wp_create_nonce( 'wp_rest' ),
    	'type' => 'columns',
    	'form_ids' => '1'
    );
    
    // Need session cookies passed to verify nonce
    $cookies = array();
    foreach ( $_COOKIE as $name => $value ) {
        $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
    }
    
    $options = array(
    	'method'.     => 'GET',
    	'body'        => $body,
    	'headers'     => array(
    		'Cache-Control' => 'no-cache',
    	),
    	'timeout'     => 60,
    	'redirection' => 5,
    	'blocking'    => true,
    	'httpversion' => '1.0',
    	'sslverify'   => false,
    	'data_format' => 'body',
    	'cookies'     => $cookies
    );
    
    $response = wp_remote_post( $endpoint, $options );

    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

  5. Skip to note 7 content

    Handle errors using is_wp_error() as follows:

    $args = array(
        'headers' => array(
            'Authorization' => 'Bearer 123hash',
            'Content-Type' => 'application/json',
        ),
        'timeout' => 0.001, // This should give you a timeout error, unless you are hitting something spectacularly fast
        'body' => json_encode( array( 'blah' => 'yack' ) ),
    );
    
    $response = wp_remote_post( 'https://developer.wordpress.org', $args );
    
    if ( ! is_wp_error( $response ) ) {
        $body = json_decode( wp_remote_retrieve_body( $response ), true );
        return $body;
    } else {
        $error_message = $response->get_error_message();
        throw new Exception( $error_message );
    }

You must log in before being able to contribute a note or feedback.