WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/wp remote post

Contents

Description

Retrieves a URL using the HTTP POST method, returning results in an array. Results include HTTP headers and content.

See wp_remote_get() for using the HTTP GET method.

NOTE: Request cookies need to be passed as an array of WP_Http_Cookie objects. See example below.

Usage

 <?php wp_remote_post$url$args ); ?> 

Parameters

$url
(string) (required) Universal Resource Locator (URL).
Default: None
$args
(array) (optional) Optional. See HTTP_API#Other_Arguments for argument details.
Default: method: POST, timeout: 5, redirection: 5, httpversion: 1.0, blocking: true, headers: array(), body: null, cookies: array()

Return Values

(array|WP_Error) 
Array of results including HTTP headers or WP_Error if the request failed.

print_r() results of requesting a simple web page using default arguments:

Array
(
    [headers] => Array
        (
            [date] => Thu, 30 Sep 2010 15:16:36 GMT
            [server] => Apache
            [x-powered-by] => PHP/5.3.3
            [x-server] => 10.90.6.243
            [expires] => Thu, 30 Sep 2010 03:16:36 GMT
            [cache-control] => Array
                (
                    [0] => no-store, no-cache, must-revalidate
                    [1] => post-check=0, pre-check=0
                )

            [vary] => Accept-Encoding
            [content-length] => 1641
            [connection] => close
            [content-type] => application/php
        )
    [body] => <html>This is a website!</html>
    [response] => Array
        (
            [code] => 200
            [message] => OK
        )

    [cookies] => Array
        (
        )

)

Examples

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.

Change Log

Source File

wp_remote_post() is located in wp-includes/http.php.

Related

HTTP API: wp_remote_request(), wp_remote_get(), wp_remote_post(), wp_remote_head() wp_remote_retrieve_body(), wp_remote_retrieve_header(), wp_remote_retrieve_headers(), wp_remote_retrieve_response_code(), wp_remote_retrieve_response_message()