我正试图通过以下代码从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产品等一起使用......那就是功能上的。但是通过订阅,我收到了这个错误:
致命错误:未捕获的异常'Automattic \ WooCommerce \ HttpClient \ HttpClientException',并在/ var / www / vhosts / magazzinoperfetto中显示消息'错误:Nessun percorso fornisce una corrispondenza tra l'URL elemodalitàdirichiesta [rest_no_route]'。它/ httpdocs / 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-&gt; lookForErrors(Array)#1 /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient .php(385):Automattic \ WooCommerce \ HttpClient \ HttpClient-&gt; processResponse()#2 /var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/Client.php(82) :Automattic \ WooCommerce \ HttpClient \ HttpClient-&gt; request('subscriptions','GET',Array,Array)#3 /var/www/vhosts/magazzinoperfetto.it/httpdocs/chang e-sottoscrizione.php(22):第324行的/var/www/vhosts/magazzinoperfetto.it/httpdocs/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php中的Automattic \ WooCommerce \
答案 0 :(得分:3)
你必须创建像get_subcription
这样的新端点并使用此函数创建一次回调函数,你可以为get订阅编写代码并从回调函数返回它。
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');
}