'API request failed: ' . $response->get_error_message() ); } $code = wp_remote_retrieve_response_code( $response ); $msg = wp_remote_retrieve_response_message( $response ); $body = wp_remote_retrieve_body( $response ); if ( $code < 200 || $code >= 300 ) { return array( 'error' => "HTTP $code $msg", 'body' => $body, ); } $decoded = json_decode( $body, true ); if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { return $decoded; } return array( 'error' => 'Invalid JSON response from API' ); } /** * Get the request arguments including headers. * * @param string $key The API key for authentication. * @return array The request arguments. */ public static function get_request_args( string $key ): array { $headers = array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-API-KEY' => $key, 'X-Client-Application' => 'WooCommerce', 'X-Client-Website' => esc_url_raw( home_url( '/' ) ), ); return array( 'headers' => $headers, 'timeout' => 15, ); } }