wp_remote_retrieve_body( array|WP_Error $response ): string

Retrieve only the body from the raw response.

Parameters

$responsearray|WP_Errorrequired
HTTP response.

Return

string The body of the response. Empty string if no body or incorrect parameter given.

Source

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

	return $response['body'];
}

Changelog

VersionDescription
2.7.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Example

    Retrieving data from a JSON API:

    $url      = 'http://example.org/api';
    $response = wp_remote_get( esc_url_raw( $url ) );
    
    /* Will result in $api_response being an array of data,
    parsed from the JSON response of the API listed above */
    $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
  2. Skip to note 4 content

    Better performance?

    Skip the extra function call and get the body (or any other part of the returned array) from wp_remote_get() . That’s essentially what wp_remote_retrieve_body() is doing.

    $url  = 'https://example.com/';
    $body = wp_remote_get( esc_url_raw( $url ) )['body'];

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