Automattic\WooCommerce\Internal\Admin\Settings

Utils::rest_endpoint_get_requestpublic staticWC 1.0

Get data from a WooCommerce API endpoint.

Method of the class: Utils{}

No Hooks.

Returns

Array|\WP_Error. The response data or a WP_Error object.

Usage

$result = Utils::rest_endpoint_get_request( $endpoint, $params );
$endpoint(string) (required)
Endpoint.
$params(array)
Params to pass with request query.
Default: array()

Utils::rest_endpoint_get_request() code WC 10.3.3

public static function rest_endpoint_get_request( string $endpoint, array $params = array() ) {
	$request = new \WP_REST_Request( 'GET', $endpoint );
	if ( $params ) {
		$request->set_query_params( $params );
	}

	// Do the internal request.
	// This has minimal overhead compared to an external request.
	$response = rest_do_request( $request );

	$server        = rest_get_server();
	$response_data = json_decode( wp_json_encode( $server->response_to_data( $response, false ) ), true );

	// Handle non-200 responses.
	if ( 200 !== $response->get_status() ) {
		return new \WP_Error(
			'woocommerce_settings_payments_rest_error',
			sprintf(
				/* translators: 1: the endpoint relative URL, 2: error code, 3: error message */
				esc_html__( 'REST request GET %1$s failed with: (%2$s) %3$s', 'woocommerce' ),
				$endpoint,
				$response_data['code'] ?? 'unknown_error',
				$response_data['message'] ?? esc_html__( 'Unknown error', 'woocommerce' )
			),
			$response_data
		);
	}

	// If the response is 200, return the data.
	return $response_data;
}