wp_remote_retrieve_response_message( array|WP_Error $response )

Retrieve only the response message from the raw response.


Description Description

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


Top ↑

Parameters Parameters

$response

(array|WP_Error) (Required) HTTP response.


Top ↑

Return Return

(string) The response message. Empty string on incorrect parameter given.


Top ↑

Source Source

File: wp-includes/http.php

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

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


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Drew Jaynes

    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.