wp_remote_retrieve_response_message( array|WP_Error $response ): string

Retrieves only the response message from the raw response.

Description

Will return an empty string if incorrect parameter value is given.

Parameters

$responsearray|WP_Errorrequired
HTTP response.

Return

string The response message. Empty string if incorrect parameter given.

Source

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

	return $response['response']['message'];
}

Changelog

VersionDescription
2.7.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Make Request and Display Error

    /**
     * Get movie information from IMDB.
     *
     * @param string $title Title of the movie
     * @param int $id IMDB ID of the movie
     * @return string|WP_Error Returns the contents of the response on success, WP_Error on failure
     */
    function wcpdx_get_movie( $title, $id = 0 ) {
    
    	// Collect the args
    	$params = array(
    		'i' => absint( $id ),
    		't' => sanitize_text_field( $title )
    	);
    
    	// Generate the URL
    	$url = 'http://www.imdbapi.com/';
    	$url = add_query_arg( $params, esc_url_raw( $url ) );
    
    	// Make API request
    	$response = wp_remote_get( esc_url_raw( $url ) );
    
    	// Check the response code
    	$response_code       = wp_remote_retrieve_response_code( $response );
    	$response_message = wp_remote_retrieve_response_message( $response );
    
    	if ( 200 != $response_code && ! empty( $response_message ) ) {
    		return new WP_Error( $response_code, $response_message );
    	} elseif ( 200 != $response_code ) {
    		return new WP_Error( $response_code, 'Unknown error occurred' );
    	} else {
    		return wp_remote_retrieve_body( $response );
            }
    }
    
    // Make request
    $movie = 'Hairspray';
    $response = wcpdx_get_movie( $movie );
    
    // Print error if error, otherwise print information
    if ( is_wp_error( $response ) ) {
    	echo 'The following error occurred when contacting IMDB: ' . wp_strip_all_tags( $response->get_error_message() );
    } else {
    	$data = json_decode( $response );
    	echo 'The movie ' . esc_html( $data['Title'] ) . ' was released in ' . absint( $data['Year'] ) . '.';
    }

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