admin管理员组

文章数量:1318335

I using remote payment system of 2 WordPress site, 1st WordPress site to 2nd WordPress site. 1st is main website & 2nd website work like merchant website that process payment of paypal. We fetch user order details of 1st website to 2nd website for process paypal payment. But on fetching web page of 2nd website getting error, but remember it solved if reload it once

Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /../plugins/rm-payment.php:on line 231

 $response = wp_remote_post( $remote_url, $request );

        if(strlen($response["body"]) > 0) {

            $insert = $wpdb->insert( $table_name, array(
                'user_email' => $query['user'], 
                'reference' => $query['ref'], 
                'token_ref' => $token_ref, 
                'point_type' => $query['ctype'],
                'amount' => $query['amount'], 
                'cost' => $total_price,
                'ref_id' => $timestamp,
                'payment_date' => current_time( 'mysql' ),
                'created_at' => current_time( 'mysql' )
            ));

            $insert2 = $A_wpdb->insert( $A_table_name, array(
                'user_email' => $query['user'], 
                'reference' => $query['ref'], 
                'amount' => $query['amount'], 
                'ref_id' => $timestamp,
                'payment_date' => current_time( 'mysql' ),
                'created_at' => current_time( 'mysql' )
            ));

            if($insert) {
                $payment_id = $wpdb->insert_id;
            } else {
                echo "Error!"; exit();
            }
        } else {
            wp_redirect( $query['url'] . '?message=user-not-exist' ); exit();
        }

'''

I using remote payment system of 2 WordPress site, 1st WordPress site to 2nd WordPress site. 1st is main website & 2nd website work like merchant website that process payment of paypal. We fetch user order details of 1st website to 2nd website for process paypal payment. But on fetching web page of 2nd website getting error, but remember it solved if reload it once

Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /../plugins/rm-payment.php:on line 231

 $response = wp_remote_post( $remote_url, $request );

        if(strlen($response["body"]) > 0) {

            $insert = $wpdb->insert( $table_name, array(
                'user_email' => $query['user'], 
                'reference' => $query['ref'], 
                'token_ref' => $token_ref, 
                'point_type' => $query['ctype'],
                'amount' => $query['amount'], 
                'cost' => $total_price,
                'ref_id' => $timestamp,
                'payment_date' => current_time( 'mysql' ),
                'created_at' => current_time( 'mysql' )
            ));

            $insert2 = $A_wpdb->insert( $A_table_name, array(
                'user_email' => $query['user'], 
                'reference' => $query['ref'], 
                'amount' => $query['amount'], 
                'ref_id' => $timestamp,
                'payment_date' => current_time( 'mysql' ),
                'created_at' => current_time( 'mysql' )
            ));

            if($insert) {
                $payment_id = $wpdb->insert_id;
            } else {
                echo "Error!"; exit();
            }
        } else {
            wp_redirect( $query['url'] . '?message=user-not-exist' ); exit();
        }

'''

Share Improve this question asked Oct 28, 2020 at 13:50 MidasMidas 1 1
  • 1 In the given code, which line is 231? The provided error tells us that on line 231 we have a WP_Error Object instead of an array. – Howdy_McGee Commented Oct 28, 2020 at 14:11
Add a comment  | 

1 Answer 1

Reset to default 0

My guess is that you're accessing $response as an array when it's a \WP_Error. Check out the possible return values of wp_remote_post:

(array|WP_Error) The response or WP_Error on failure.

You should modify your code in a couple of ways:

$response = wp_remote_post( $remote_url, $request );

if ( is_wp_error( $response ) ) {
    // handle the error response here.
}

$body = wp_remote_retrieve_body( $response );

if( false !== strlen( $body ) ) {

This ensures you catch any \WP_Error returns and gets the body data using wp_remote_retrieve_body. You can also use wp_remote_retrieve_response_code( $response ) to get the HTTP status code (e.g. 200, 401, etc.)

本文标签: wp queryFatal error Uncaught Error Cannot use object of type WPError as array in pluginsrmpaymentphp