Home » Functions » wp_remote_retrieve_body

Function Name: wp_remote_retrieve_body

WordPress is a popular content management system that allows users to create websites and blogs easily. One of the most important features of WordPress is its ability to interact with remote servers. The wp_remote_retrieve_body function is a handy function that retrieves the body of a response returned by a remote server.

When you send a request to a remote server using the wp_remote_request function, the server will respond with a status code, headers, and a body. The body contains the actual content of the response, such as HTML, JSON, or XML.

The wp_remote_retrieve_body function retrieves the body content from the response object returned by wp_remote_request. This function is particularly useful when you need to extract data from the body content of a remote response.

Example usage code:

$response = wp_remote_request( ‘https://www.example.com/api/data.json‘ ); if( is_wp_error( $response ) ) { echo "Error retrieving data"; } else { $data = json_decode( wp_remote_retrieve_body( $response ) ); // Process the data … }

In this example, we are using wp_remote_request to send a request to a remote server to retrieve data in JSON format. Once we receive the response, we check if there was an error. If there was no error, we use wp_remote_retrieve_body to extract the body content of the response and decode it using json_decode. Finally, we can process the data returned by the remote server.

Overall, wp_remote_retrieve_body is a useful function to retrieve the body content of a response returned by a remote server when developing WordPress websites and applications.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.