WordPress at Your Fingertips

wp_remote_post()WP 2.7.0

Gets a remote page using the HTTP POST method. The result includes HTTP headers and content of the webpage and returned in the form of an array. It's a wrapper for curl.

You can use cookies: request cookies need to be passed as an array of WP_Http_Cookie objects.

Use wp_remote_get() for HTTP GET method.

No Hooks.

Return

Array|WP_Error. The response or WP_Error on failure.

If a server returned 500, 404 etc. status codes, there won't be a WP_Error – the data of the response will be returned as usual.

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>HTML code of the webpage!</html>
	[response] => Array (
			[code]    => 200
			[message] => OK
		)

	[cookies] => Array()

)

Usage Template

wp_remote_post( $url, array(
	'timeout'     => 5,
	'redirection' => 5,
	'httpversion' => '1.0',
	'blocking'    => true,
	'headers'     => array(),
	'body'        => null, // request parameters in an array
	'cookies'     => array()
) );

Usage

wp_remote_post( $url, $args );
$url(string) (required)
Site URL to retrieve.
$args(array)

Request arguments.

  • body – an array of request parameters where key is the name of the parameter and the value is its value: array( 'foo'=>'val', 'bar'=>'val' )

  • timeout — time in seconds, before the connection is dropped and an error is returned. The time can be changed in the plugin through the filter http_request_timeout.
    Default: 5

  • redirection — the amount of times to follow a redirect before giving up. There is a filter for the value: http_request_redirection_count.
    Default: 5

  • user-agent — it allows you to set the user-agent. There is a filter for changing this value: http_headers_useragent.
    Default: "WordPress/4.0; http://example.com"

  • blocking — allows you to trigger a non-blocking request. The default is true; setting it to false will generally allow PHP to continue execution while the transport is working. The key is that when you set blocking to false, then it will just send the request and won't bother you with the details. This is useful for sending a POST request, where you aren't concerned with whether it succeeded or not, or if you don't want to slow down the processing time of the page. (Note that not all the transports support non-blocking requests, and so you may still be blocked anyway. The alternative of setting an ultra-low timeout is not recommended, since low timeouts may cause the request to not be sent at all with some transports.)
    Default: true

  • compress — allows you to send the request body as compressed.
    Default: true

  • decompress — Responsible for accept-encoding request header. If you do leave this as true, then the headers will be set to tell the server that compressed data is accepted and it will be decompressed in the response. If you sent it to false, then the header will be sent that tells servers that compressed content is not accepted. If the content is still compressed, because of incorrect implementation or you sent the headers for it, then you will need to decompress the content.
    Default: true

  • sslverify — true - will check to see if the SSL certificate is valid (not self-signed, actually for the site in the request) and will deny the response if it isn't. If you are requesting HTTPS and know that the site is self-signed or is invalid and are reasonably sure that it can be trusted, then set to false.
    Default: true

See wp_remote_request() for the full list of arguments.

Examples

0

#1 Send POST data to a remote URL

$url = 'http://example.com/profile';
$args = array(
	'timeout'     => 45,
	'redirection' => 5,
	'httpversion' => '1.0',
	'blocking'    => true,
	'headers'     => array(),
	'body'        => array( 'username' => 'bob', 'password' => '1234xyz' ),
	'cookies'     => array()
);
$response = wp_remote_post( $url, $args );

// error check
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>';
}

$response['body'] here contains HTML of the page.

Notes

Changelog

Since 2.7.0 Introduced.

wp_remote_post() code WP 6.4.3

function wp_remote_post( $url, $args = array() ) {
	$http = _wp_http_get_object();
	return $http->post( $url, $args );
}
vladlu 100vlad.lu
Editors: Kama 240
1 comment
    Log In