wp_remote_retrieve_body( array|WP_Error $response ): string

Retrieve only the body from the raw response.


Parameters

$response array|WP_Error Required
HTTP response.

Top ↑

Return

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


Top ↑

Source

File: wp-includes/http.php. View all references

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

	return $response['body'];
}


Top ↑

Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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 2 content
    Contributed by Sergio Scabuzzo

    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.