Reputation: 103
I'm trying to get all subscriptions from php with this code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://www.magazzinoperfetto.it', // Your store URL
'ck_c92b6b6452XXXXXXXXXXXXXXXXXX',
'cs_e3380e1c07XXXXXXXXXXXXXXXXXX',
[
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v2' // WooCommerce WP REST API version
]
);
print_r($woocommerce->get('subscriptions'));
?>
If I use the api with woocommerce product etc... it's functionally. But with the subscription I receive this error:
Fatal error: Uncaught exception 'Automattic\WooCommerce\HttpClient\HttpClientException' with message 'Error: Nessun percorso fornisce una corrispondenza tra l'URL e le modalità di richiesta [rest_no_route]' in /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:324 Stack trace: #0 /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(349): Automattic\WooCommerce\HttpClient\HttpClient->lookForErrors(Array) #1 /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(385): Automattic\WooCommerce\HttpClient\HttpClient->processResponse() #2 /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request('subscriptions', 'GET', Array, Array) #3 /var/www/vhosts/magazzinoperfetto.it/httpdocs/change-sottoscrizione.php(22): Automattic\WooCommerce\ in /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 324
Upvotes: 4
Views: 844
Reputation: 822
you have to create the new endpoint like get_subcription
and create the once callback function with this function you can write your code for the get subscription and return it from the callback function.
add_action( 'rest_api_init', 'custom_api_endpoints' );
function custom_api_endpoints () {
register_rest_route( 'wc/v2', 'get_subcription', array(
'methods' => 'POST',
'callback' => 'custom_subscription_endpoint_handler'
) );
}
function custom_subscription_endpoint_handler () {
return $woocommerce->get('subscriptions');
}
Upvotes: 3