wp_remote_post( string $url, array $args = array() )

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


Description Description

See also See also


Top ↑

Parameters Parameters

$url

(string) (Required) URL to retrieve.

$args

(array) (Optional) Request arguments.

Default value: array()


Top ↑

Return Return

(WP_Error|array) The response or WP_Error on failure.


Top ↑

Source Source

File: wp-includes/http.php

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

Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.


Top ↑

User Contributed Notes 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 4 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 );
    

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