Automattic\WooCommerce\Utilities

RestApiUtil::get_endpoint_data()publicWC 9.0.0

Get data from a WooCommerce API endpoint. This method used to be part of the WooCommerce Legacy REST API.

Method of the class: RestApiUtil{}

No Hooks.

Return

Array|\WP_Error.

Usage

$RestApiUtil = new RestApiUtil();
$RestApiUtil->get_endpoint_data( $endpoint, $params );
$endpoint(string) (required)
Endpoint.
$params(array)
Params to pass with request.
Default: array()

Changelog

Since 9.0.0 Introduced.

RestApiUtil::get_endpoint_data() code WC 9.4.2

public function get_endpoint_data( $endpoint, $params = array() ) {
	$request = new \WP_REST_Request( 'GET', $endpoint );
	if ( $params ) {
		$request->set_query_params( $params );
	}
	$response = rest_do_request( $request );
	$server   = rest_get_server();
	$json     = wp_json_encode( $server->response_to_data( $response, false ) );
	return json_decode( $json, true );
}