Automattic\WooCommerce\Internal\Admin\Settings

Utils::rest_endpoint_post_requestpublic staticWC 1.0

Post data to a WooCommerce API endpoint and return the response data.

Метод класса: Utils{}

Хуков нет.

Возвращает

Массив|\WP_Error. The response data or a WP_Error object.

Использование

$result = Utils::rest_endpoint_post_request( $endpoint, $params );
$endpoint(строка) (обязательный)
Endpoint.
$params(массив)
Params to pass with request body.
По умолчанию: array()

Код Utils::rest_endpoint_post_request() WC 10.9.4

public static function rest_endpoint_post_request( string $endpoint, array $params = array() ) {
	$request = new \WP_REST_Request( 'POST', $endpoint );
	if ( $params ) {
		$request->set_body_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 POST %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;
}