wp_remote_retrieve_body()
Retrieves the content (body) of a response which was retrieved by any of the functions like wp_remote_*(), for example wp_remote_get().
This function accepts only one parameter, the response of any of the wp_remote_*()
functions, such as wp_remote_get().
Use wp_remote_retrieve_response_code() when only a response code (like 200) is needed.
No Hooks.
Return
String
.
String
- response content (body).Empty string
- when the answer is empty or WP_Error is passed to $response parameter.
Usage
wp_remote_retrieve_body( $response );
- $response(array|WP_Error) (required)
The response received with one of the WP HTTP API functions, for example: wp_remote_get().
The function can accept an WP_Error error object.
Examples
#1 Getting the response body
Let's get the data of a Github user:
$response = wp_remote_get( 'https://api.github.com/users/doiftrue' ); $body = wp_remote_retrieve_body( $response ); // decode data $api_response = json_decode( $body, true ); print_r( $api_response ); /* displays: Array ( [login] => doiftrue [id] => 15121552 [node_id] => MDQ6VXNlcjE1MTIxNTUy [avatar_url] => https://avatars.githubusercontent.com/u/15121552?v=4 [gravatar_id] => [url] => https://api.github.com/users/doiftrue [html_url] => https://github.com/doiftrue [followers_url] => https://api.github.com/users/doiftrue/followers [following_url] => https://api.github.com/users/doiftrue/following{/other_user} [gists_url] => https://api.github.com/users/doiftrue/gists{/gist_id} [starred_url] => https://api.github.com/users/doiftrue/starred{/owner}{/repo} [subscriptions_url] => https://api.github.com/users/doiftrue/subscriptions [organizations_url] => https://api.github.com/users/doiftrue/orgs [repos_url] => https://api.github.com/users/doiftrue/repos [events_url] => https://api.github.com/users/doiftrue/events{/privacy} [received_events_url] => https://api.github.com/users/doiftrue/received_events [type] => User [site_admin] => [name] => Kama [company] => [blog] => https://wp-kama.ru [location] => [email] => [hireable] => [bio] => [twitter_username] => [public_repos] => 29 [public_gists] => 2 [followers] => 14 [following] => 2 [created_at] => 2015-10-14T08:52:55Z [updated_at] => 2022-02-14T12:17:11Z ) */
#2 Retrieve a remote page and caching it
function get_remote_html( $url, $trans_name = 'foo_remote_html' ) { // Let's check the transit option, if it's missing get remote HTML $html = get_transient( $trans_name ); if( false === $html ){ // get HTML $response = wp_remote_get( $url ); // Check for errors if ( is_wp_error( $response ) ) { return; } // Get the body $html = wp_remote_retrieve_body( $response ); // Write the received request into the transit option for 24 hours set_transient( $trans_name, $html, 24 * HOUR_IN_SECONDS ); } return $html; } // call the function $html = get_remote_html( 'http://example.com/some-remote-file.html', 'my_trans_name' );
Changelog
Since 2.7.0 | Introduced. |
wp_remote_retrieve_body() wp remote retrieve body code WP 6.8
function wp_remote_retrieve_body( $response ) { if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) { return ''; } return $response['body']; }