admin管理员组

文章数量:1122846

I am getting the following error in the code snippet below and pointing to the line that references the $result[ 'body' ] assignment. The is_wp_error should have caught the error?

Fatal error: Cannot use object of type WP_Error as array

$result = wp_remote_post( ..... ) );
if ( is_wp_error( $result ) ):
    $display = 'Error Message';
else:
    $display = $result[ 'body' ];
endif;

I am getting the following error in the code snippet below and pointing to the line that references the $result[ 'body' ] assignment. The is_wp_error should have caught the error?

Fatal error: Cannot use object of type WP_Error as array

$result = wp_remote_post( ..... ) );
if ( is_wp_error( $result ) ):
    $display = 'Error Message';
else:
    $display = $result[ 'body' ];
endif;
Share Improve this question edited Sep 4, 2024 at 11:03 bueltge 17.1k7 gold badges61 silver badges97 bronze badges asked May 29, 2017 at 3:40 user1941464user1941464 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Both wp_remote_post and wp_remote_get return WP_Error object if there is an error. You could use the get_error_message function of WP_Error class to receive the error and show it.

$request = wp_remote_post( $url );
if ( is_wp_error( $request ) ) {
    // If the request has failed, show the error message
    echo $request->get_error_message();
} else {
    $content = wp_remote_retrieve_body( $request );
    // Do stuff with $content
}

For more details, go ahead Here

本文标签: iswperror is missing error