woocommerce / wc-api-php

WooCommerce REST API PHP Library
https://packagist.org/packages/automattic/woocommerce
MIT License
528 stars 160 forks source link

Exception 'Automattic\WooCommerce\HttpClient\HttpClientException' with message 'JSON ERROR: Syntax error' #266

Open Er-Kalpesh opened 4 years ago

Er-Kalpesh commented 4 years ago

Exception 'Automattic\WooCommerce\HttpClient\HttpClientException' with message 'JSON ERROR: Syntax error'

in \vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php:378

Stack trace:
#0 \vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php(422): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
#1 \vendor\automattic\woocommerce\src\WooCommerce\Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request('products', 'GET', Array, Array)
#2 \common\components\Woocommerce.php(56): Automattic\WooCommerce\Client->get('products')
#3 \modules\woocommerce\controllers\DefaultController.php(90): common\components\Woocommerce->getAndSaveProductsFromWoocommerce()
#4 [internal function]: modules\woocommerce\controllers\DefaultController->actionGetAllProductsFromWoocommerce()
#5 \vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array(Array, Array)
#6 \vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#7 \vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('get-all-product...', Array)
#8 \vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('woocommerce/def...', Array)
#9 \vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#10 \backend\web\index.php(17): yii\base\Application->run()
#11 {main}
gsusalvarado commented 4 years ago

Hello, any solution @Er-Kalpesh Er-Kalpesh I have the same problem?

MasterPCUK commented 4 years ago

Hello, any solution for this? I just ran in to this problem with fresh Wordpress and WooCommerce + Storefront install. Migrated products over via csv import suite. And boom same error.

        $woocommerce = new Client(
            'https://mydomain.co.uk/',
            'ck_xxxx',
            'cs_xxxx'
        );

        print_r($woocommerce->get('products/9098') );

Automattic\WooCommerce\HttpClient\HttpClientException JSON ERROR: Syntax error

Funny thing is that it works just fine on my Roots Bedrock WP installation, but not on default WP. I'm out of ideas for a day.

MasterPCUK commented 4 years ago

Hello, any solution for this? I just ran in to this problem with fresh Wordpress and WooCommerce + Storefront install. Migrated products over via csv import suite. And boom same error.

      $woocommerce = new Client(
          'https://mydomain.co.uk/',
          'ck_xxxx',
          'cs_xxxx'
      );

      print_r($woocommerce->get('products/9098') );

Automattic\WooCommerce\HttpClient\HttpClientException JSON ERROR: Syntax error

Funny thing is that it works just fine on my Roots Bedrock WP installation, but not on default WP. I'm out of ideas for a day.

Turns out this was an issue with permalinks! I had to dump the url from processResponse, turns out if you have default permalinks, /wp-json/wc/v3/products/ does not exist.

hgskvad commented 4 years ago

I am using the right permalinks structure but still have this issue. But when I manually log in to my WP site and then try again, it works. After some time, when I logged out from the WP admin, it is not working again.

indraejago commented 4 years ago

Hello, any solution? I have the same problem error log: [07-Oct-2020 15:06:26 UTC] PHP Notice: register_rest_route was called incorrectly. The REST API route definition for media-cleaner/v1/clear_logs is missing the required permission_callback argument. For REST API routes that are intended to be public, use __return_true as the permission callback. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in /home/public_html/wp-includes/functions.php on line 5225

hgskvad commented 4 years ago

For me, it was a security plugin which changed the .htaccess content and blocked the request.

AdrienPerinot commented 4 years ago

FWIW, I had the same problem a few minutes ago. It was actually due to the fact that I had set up both keys but had not changed the URL (in the example above, 'https://mydomain.co.uk/',) After changing to the correct URL, everything was working again.

ErvinSabic commented 3 years ago

Can confirm this is a permalinks issue. Changed mine to pretty and API access was restored.

xuandung38 commented 3 years ago

Solution : Just change default settings for permalink in wp-admin/options-permalink.php

vincent-lu commented 2 years ago

Having the same problem, happens from time to time with the same same code & setup

z4yd4 commented 2 years ago

Not sure if this will be any help for you :/

I've encountered the same issues with one of our apps and we found out that in most cases it was because of some store/server settings or Security plugins (iThemes Security, WordFence, WP Tiger,...).

I've received several customer responses:

1.) ​​ "I can confirm that disabling the “prevent discovery” in the the brute force section temporarily allowed me to connect to the app."

2.) "Problem source is Wordfence and another security plugins. When we create new REST API key&secret and scan on app, our security plugin Wordfence blocked this registration process. After that we follow this steps:

Go to WordFence "All Options" page
Click to "Additional Options"
Uncheck the option "Prevent discovery of usernames through '/?author=N' scans, the oEmbed API, the WordPress REST API, and WordPress XML Sitemaps"
Scan new API key's QR code from Wordpress REST API page with mobile app.
Wordfence plugin added this initial process to whitelist.
Check the option "Prevent discovery of usernames through '/?author=N' scans, the oEmbed API, the WordPress REST API, and WordPress XML Sitemaps" for security."

3.) "First deactivate the plugin wordfence and then do all the steps that you indicate on the app. Login to your Woocommerce, Create API and scan QR code. After that will work and connect with your store. When this happens you can now re-activate the wordfence plugin.​"

4.) WP Tiger "I managed to make things work. I had another security plugin at the hosting level (WP Tiger)."

5.) ModSec "Our server support revealed that the request was hitting a ModSec rule. This can be disabled on the domain level; the request can then be made and then the rule can be enabled once more."

Hope this helps -I apologize if not :/

merciful-player commented 6 months ago

I've been using the WP API without any issues for about 2 yrs now, but since May 1, 2024, I've been getting the following error.

  1. Has anyone figured out the solution to this problem? And by the way, the product number 446611 does exist.
  2. Can someone tell me what a "Pretty Permanlink" looks like?
PHP Fatal error:  Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: JSON ERROR: Syntax error in /public_html/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:411
Stack trace:
#0 /public_html/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(455): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
#1 /public_html/vendor/automattic/woocommerce/src/WooCommerce/Client.php(69): Automattic\WooCommerce\HttpClient\HttpClient->request('products/446611', 'PUT', Array)
#2 /public_html/php-scripts/inventory/Testing.php(356): Automattic\WooCommerce\Client->put('products/446611', Array)
#3 {main}
  thrown in /public_html/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 411

HELP!!

sudhakar-mwb commented 1 week ago

I have changed the default permalink settings in wp-admin/options-permalink.php, but the same issue recurs intermittently, even with the same code and setup.

PHP Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: JSON ERROR: Syntax error in /public_html/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:411 Stack trace:

0 /public_html/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(455): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()

1 /public_html/vendor/automattic/woocommerce/src/WooCommerce/Client.php(69): Automattic\WooCommerce\HttpClient\HttpClient->request('products/446611', 'PUT', Array)

2 /public_html/php-scripts/inventory/Testing.php(356): Automattic\WooCommerce\Client->put('products/446611', Array)

3 {main}

thrown in /public_html/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 411