4

我正在尝试使用以下代码从 php 获取所有订阅:

<?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'));    
?>

如果我将 api 与 woocommerce 产品等一起使用......它是功能性的。但是通过订阅我收到此错误:

致命错误:/var/www/vhosts/magazzinoperfetto.it/httpdocs 中的未捕获异常“Automattic\WooCommerce\HttpClient\HttpClientException”和消息“错误:Nessun percorso fornisce una corrispondenza tra l'URL e le modalità di richiesta [rest_no_route]” /vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:324 堆栈跟踪:#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\ 在 /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php 在第 324 行

4

1 回答 1

3

您必须创建新的端点,get_subcription并使用此函数创建一次回调函数,您可以为获取订阅编写代码并从回调函数返回它。

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');
}
于 2017-10-11T12:55:03.473 回答