laravel-woocommerce
laravel-woocommerce copied to clipboard
JSON ERROR: Syntax error
Sometimes may be you get this error. If you are using it first time then make sure you followed some steps
- Enable
mod_rewrite
in your server - 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>
- Make sure your URL
permalink
isPost name
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?
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.
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.
@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.
@SDekkers You don't need to use HTTPClient
. It already implemented. Can you share your code how yo implement this package?
@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)
@SDekkers Sounds looks good. But where you get wocommerce_id
in above example?
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.
@SDekkers Yea, you get the point. Go ahead. It's not this package related issue.
Hey there I still have the similar issue, any advise would be appreciated. regards laravel 7 - php 7.4
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
thanks for the response, It was working as intended almost a week ago. no changes no updates, nothing. Maybe a webhosting related issue ?
Faced same issue.
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.
Same problem here, new Woocommerce installation set up locally, but can't establish a connection.
Hello @AlMahn,
Can you share the error? Thanks
@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
@saqlainkhadimbusiness
Please share your request and response. Thanks
Changing the store url from https://example.com
to https://www.example.com
(so adding www) fixed this issue for me.