API REST Woocommerce – JSON ERROR
-
Hello,
I already have some experience in programming in wordpress along with online stores and integrations with software + online store. We recently created an integration for an online store using the woocommerce api to make the calls, since there is already something for this purpose, we have to take advantage of it.Turns out, these calls to the woocommerce api constantly give the Syntax Json error which makes it a little tiring since it’s not programming errors.
I’ll give an example, I have the authentication working correctly using the code below:
// AUTH require __DIR__ . '/vendor/autoload.php'; use Automattic\WooCommerce\Client; $woocommerce = new Client( API_URL, CONSUMER_KEY, CONSUMER_SECRET, [ 'wp_api' => true, 'version' => 'wc/v3', 'query_string_auth' => true, 'verify_ssl' => false ] ); // END: AUTH
And I will query the information of an existing product using the following code (where the $pid variable returns the product id):
print_r($woocommerce->get('products/'.$pid));
Only in this example above, it is all correct according to the api documentation at this URL https://woocommerce.github.io/woocommerce-rest-api-docs/. Just using the get function to return the product information I want gives the following error, every time. There are times it works, other times it keeps giving the same error and it’s not because the permalinks aren’t changed because in fact they are, the permalinks don’t change by themselves. Error:
Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: JSON ERROR: Syntax error in /media/servidordev/www/SITE_XPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:411 Stack trace: #0 /media/servidordev/www/SITE_XPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(455): Automattic\WooCommerce\HttpClient\HttpClient->processResponse() #1 /media/servidordev/www/SITE_XPTO/import/vendor/automattic/woocommerce/src/WooCommerce/Client.php(69): Automattic\WooCommerce\HttpClient\HttpClient->request() #2 /media/servidordev/www/SITE_XPTO/import/import.php(581): Automattic\WooCommerce\Client->put() #3 /media/servidordev/www/SITE_XPTO/import/import.php(637): update_produto() #4 /media/servidordev/www/SITE_XPTO/import/import.php(700): import_produtos() #5 /media/servidordev/www/SITE_XPTO/import/import.php(706): run_only_product() #6 {main} thrown in /media/servidordev/www/SITE_XPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 411
This import.php file, which contains the api and the calling functions, is added to a cronjob to read an XML every X time and send products from the software to the online store. Any idea to solve my problem?
- The topic ‘API REST Woocommerce – JSON ERROR’ is closed to new replies.