WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function API/wp remote retrieve body

This article has been requested to be merged into Function_Reference/wp_remote_retrieve_body.

Description

Retrieves the body of an already retrieved HTTP request.

See Function_API/wp_remote_get for an example of the HTTP GET method.

Usage

 <?php wp_remote_retrieve_body$response ); ?> 

Parameters

$response
(array) (required) HTTP response array from an already performed HTTP request.
Default: None

Return Values

Returns a string. If there was an error returned by the existing HTTP request or a problem with the data then a blank string will be returned.

Here is the content of the retrieved URL!

Examples

$the_body will contain the actual page content returned by the server.

$the_body = wp_remote_retrieve_body( wp_remote_get('http://example.com') );

In this example, we'll use wp_remote_get(), is_wp_error(), and finally store the remote HTML in a transient:

function get_remote_html() {

        // Check for transient, if none, grab remote HTML file
	if ( false === ( $html = get_transient( 'foo_remote_html' ) ) ) {

                // Get remote HTML file
		$response = wp_remote_get( 'http://example.com/some-remote-file.html' );

                       // Check for error
			if ( is_wp_error( $response ) ) {
				return;
			}

                // Parse remote HTML file
		$data = wp_remote_retrieve_body( $response );

                        // Check for error
			if ( is_wp_error( $data ) ) {
				return;
			}

                // Store remote HTML file in transient, expire after 24 hours
		set_transient( 'foo_remote_html', $data, 24 * HOUR_IN_SECONDS );

	}

	return $html;

}

Notes

Below is the actual function code in WordPress. As you can see it simply checks that there was no error with the HTTP response and that the body is set. If so then it returns $response['body'].

function wp_remote_retrieve_body(&$response) {
	if ( is_wp_error($response) || ! isset($response['body']) )
		return '';

	return $response['body'];
}

Change Log

Source File

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

Related