This page redirects to an external site: https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/
Languages: English • (Add your language)
Retrieves the body of an already retrieved HTTP request.
See Function_API/wp_remote_get for an example of the HTTP GET method.
<?php wp_remote_retrieve_body( $response ); ?>
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!
$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;
}
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'];
}
wp_remote_retrieve_body() is located in wp-includes/http.php.