简体   繁体   中英

WooCommerce REST API Fatal Error Connection Issue

I'm trying to make woocommerce REST API work. On first sight it apears quite simple by following the doc: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#introduction . But I struggle making it work. Here is my code:

<?php

require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'my_website_uri',
    'consumer_key',
    'consumer_secret',
    [
        'wp_api' => true,
        'version' => 'wc/v2'
    ]
);


try{
  print_r($woocommerce->get(''));
}
catch (HttpClientException $e) {
    print_r($e->getMessage());
    print_r($e->getRequest());
    print_r($e->getResponse());
}

?>

Here is what i get:

Fatal error: Uncaught Automattic\\WooCommerce\\HttpClient\\HttpClientException: Syntax error in /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:346 Stack trace: #0 /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(385): Automattic\\WooCommerce\\HttpClient\\HttpClient->processResponse() #1 /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/Client.php(82): Automattic\\WooCommerce\\HttpClient\\HttpClient->request('', 'GET', Array, Array) #2 /Applications/MAMP/htdocs/WC_REST/test.php(19): Automattic\\WooCommerce\\Client->get('') #3 {main} thrown in /Applications/MAMP/htdocs/WC_REST/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 346

I wrote down exactly what's in the doc.

I always got an '/wp-json/wc/v2/system_status was not found on this server.' error when enabling the output of $body as suggested above.

I fixed it with enabling permalinks (in admin-backend) which is needed for accessing a slash separated url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM