0
votes

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.

1
try a non-empty "get" like $woocommerce->get("system_status") - mmm
Hy @mmm. well i did, got the same error - OTmn
this client is not very verbose when there is a syntax error. to help it, go in the file "vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php" in the method processResponse, search the line if (JSON_ERROR_NONE !== json_last_error()) { and just after add echo $body; then you will see the raw response of the server. - mmm
Did this ever get resolved? I'm having the same error and have tried to look through many different solutions. Does this have anything to do with the way composer is installed or is it a totally different issue? - GDT
Heh, nice issue solving, php community - stand alone

1 Answers

0
votes

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.