Codexshaper / laravel-woocommerce

WooCommerce Rest API for Laravel
MIT License
194 stars 57 forks source link

JSON ERROR: Syntax error #55

Open maab16 opened 3 years ago

maab16 commented 3 years ago

Sometimes may be you get this error. If you are using it first time then make sure you followed some steps

  1. Enable mod_rewrite in your server
  2. Make sure you have .htaccess file in your WordPress root directory. Contents should look like
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
  3. Make sure your URL permalink is Post name
alexvolley commented 3 years ago

I have changed the .htaccess file to as shown however I'm still getting JSON ERROR: Syntax error, is there a work around for this?

SDekkers commented 3 years ago

I've found this error to show when trying to use the update call on a product, the root cause is the destination shop not accepting PUT requests (http 405 is thrown) as the server is limited to GET and POST requests.

I'll submit a PR that will show the appropriate HTTP error.

maab16 commented 3 years ago

Hello @alexvolley and @SDekkers,

Thanks for creating the issue. If you followed above configuration then it's not configuration issue.

For @alexvolley you can check your API key and secret carefully and make sure have read and write permission. For further information follow this link https://woocommerce.github.io/woocommerce-rest-api-docs/#authentication

For @SDekkers I think you need to check your wocommerce account that you have write access.

SDekkers commented 3 years ago

@maab16 The key used was able to add products, but not update them.. I was able to reverse-engineer the response given by the API, which resulted in the below

(did a dd() on $body in the function processResponse() in the HttpClient)

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n
<html><head>\n
<title>405 Method Not Allowed</title>\n
</head><body>\n
<h1>Method Not Allowed</h1>\n
<p>The requested method PUT is not allowed for this URL.</p>\n
</body></html>\n

This obviously isn't JSON so the syntax error is technically correct, but the error can be better looking at the http status code.

edit: I see the HTTPClient isn't part of this package, so I shall PR it into the other package.

maab16 commented 3 years ago

@SDekkers You don't need to use HTTPClient. It already implemented. Can you share your code how yo implement this package?

SDekkers commented 3 years ago

@maab16 Nothing special..

Product::update($product->woocommerce_id, $data);

The PR I will add to the HTTPClient class in the Automattic\WooCommerce package to improve the error message when the API endpoint throws a HTTP error (i.e. 405 method not allowed)

maab16 commented 3 years ago

@SDekkers Sounds looks good. But where you get wocommerce_id in above example?

SDekkers commented 3 years ago

Just my database, a separate modal with scraped products from a partner webshop.

The issue here is that my clients webshop hasn't allowed PUT and DELETE requests on its webserver, so the error message (JSON Syntax error) was somewhat vague.

maab16 commented 3 years ago

@SDekkers Yea, you get the point. Go ahead. It's not this package related issue.

CrawDance commented 3 years ago

Hey there I still have the similar issue, any advise would be appreciated. regards laravel 7 - php 7.4

maab16 commented 3 years ago

I've tested and it worked for me Laravel 7 and PHP 7.4. I think your issue related to the WordPress not Laravel. If you follow the instruction carefully I hope this issue will fix. Thanks

CrawDance commented 3 years ago

thanks for the response, It was working as intended almost a week ago. no changes no updates, nothing. Maybe a webhosting related issue ?

MSufianS commented 3 years ago

Faced same issue.

itsme-francesco commented 3 years ago

Hi, I'm facing the same issue.

In my case the issue is related to WP installed in a subfolder.

Is there a solution to apply in this case?

Thanks!

[Edit] I solved the issue by moving the site from subfolder to the root directory.

AIMahn commented 2 years ago

Same problem here, new Woocommerce installation set up locally, but can't establish a connection.

maab16 commented 2 years ago

Hello @AlMahn,

Can you share the error? Thanks

saqlainkhadim commented 1 year ago

@maab16 When I call the API from the request client it returns JSON and ok. but when I call this form laravel-woocomerce it says the JSON syntax error. maybe it is error in the repo or maybe my configuration fault

maab16 commented 1 year ago

@saqlainkhadimbusiness

Please share your request and response. Thanks

StanMenten99 commented 1 year ago

Changing the store url from https://example.com to https://www.example.com (so adding www) fixed this issue for me.

gusjara commented 1 year ago

Hi, i have an implementation with woocommerce version 7.1.0 and I don't have problems. But on a new implementations with woocommerce version 7.3.0 I can't fix the error "JSON ERROR: Syntax error", it's only when run update a product, but when I create I don't have any error.

The error displaying is that: #message: "JSON ERROR: Syntax error" #code: 503 #file: "./vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php" #line: 411

Tried with www and without.

Any workaround for this?

I'll try to downgrade wc version and comment later.

My best regards.

outzo commented 7 months ago

Senhores me deparei com o mesmo problema, no GET de todas as Categorias, mesma aplicação que estava rodando sem problemas dias atrás Gentlemen, I came across the same problem, in the GET of all Categories, the same application that was running without problems a few days ago

'JSON ERROR: Syntax error',
 $params = [
              'per_page' => 100,
              'page' => $cont,
            ];
            $cat1 = Category::All($params);
outzo commented 7 months ago

Olá, tenho uma implementação com woocommerce versão 7.1.0 e não tenho problemas. Mas em novas implementações com woocommerce versão 7.3.0 não consigo corrigir o erro "JSON ERROR: Syntax error", é só quando executo atualizar um produto, mas quando crio não tenho nenhum erro.

O erro exibido é que: #message: "JSON ERROR: Syntax error" #code: 503 #file: "./vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php" #line: 411

Tentei com wwwe sem.

Alguma solução alternativa para isso?

Vou tentar fazer o downgrade da versão wc e comentar mais tarde.

Os meus melhores cumprimentos.

Também tentei esta alternativa sem sucesso

gusjara commented 7 months ago

Hi, I use on this way and can get categories Use URL without 'www' only https://domain.com use use Codexshaper\WooCommerce\Facades\Category;

getter $categories = Category::all(['per_page' => 100, 'page' => $page_number]); and in my .env WOOCOMMERCE_VERIFY_SSL=false WOOCOMMERCE_API_VERSION=v3

maulik41 commented 4 months ago

I was facing same error, i solved by this - stackoverflow