• Resolved biatsalu

    (@biatsalu)


    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?

    • This topic was modified 2 years ago by biatsalu.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Igor H

    (@ihereira)

    Hello,

    It appears that there is a “Syntax error” on JSON. Mostly that happens when PHP errors are being thrown on the site, or perhaps someone forgot to disable PHP’s display_errors.

    Please check under WooCommerce > System Status > Logs (if any other error is visible, try to fix it before using the REST API).

    Hope this helps.

    Thread Starter biatsalu

    (@biatsalu)

    Hi,

    After checking the woocommerce logs I get the same json syntax error every time and it’s not a problem with the php code going from the import.php file, because there are times when it works without giving any errors. We have the display_errors also disabled

    2023-03-08T10:00:03+00:00 CRITICAL Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: JSON ERROR: Syntax error in /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:411
    Stack trace:
    #0 /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(455): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
    #1 /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/Client.php(69): Automattic\WooCommerce\HttpClient\HttpClient->request()
    #2 /media/servidordev/www/SITEXPTO/import/import.php(584): Automattic\WooCommerce\Client->put()
    #3 /media/servidordev/www/SITEXPTO/import/import.php(640): update_produto()
    #4 /media/servidordev/www/SITEXPTO/import/import.php(681): import_produtos()
    #5 /media/servidordev/www/SITEXPTO/import/import.php(708): run_import()
    #6 {main}
      thrown em /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php na linha 411
    
    2023-03-08T13:00:04+00:00 CRITICAL Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: JSON ERROR: Syntax error in /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:411
    Stack trace:
    #0 /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(455): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
    #1 /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/Client.php(69): Automattic\WooCommerce\HttpClient\HttpClient->request()
    #2 /media/servidordev/www/SITEXPTO/import/import.php(584): Automattic\WooCommerce\Client->put()
    #3 /media/servidordev/www/SITEXPTO/import/import.php(640): update_produto()
    #4 /media/servidordev/www/SITEXPTO/import/import.php(681): import_produtos()
    #5 /media/servidordev/www/SITEXPTO/import/import.php(708): run_import()
    #6 {main}
      thrown em /media/servidordev/www/SITEXPTO/import/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php na linha 411

    Hi @biatsalu

    Thanks for reaching out!

    This is a fairly complex development topic that would require customization to do it. Since custom coding is outside our scope of support, I am leaving this thread open for a bit to see if anyone can chime in to help you out.

    For questions related to development and custom coding, your best bet is to ask on any of these channels for support. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    Hope this helps!

    Thread Starter biatsalu

    (@biatsalu)

    For those who have this kind of problems, my problem was solved by increasing the php resources:

    max_execution_time I set it to 50000 and the memory_limit I set it to 2G, it never gave the error again.

    It seems that the woocommerce api pulls a lot for resources and starts giving syntax errors out of nowhere. Because when it gives a Json error, it appears behind the curl error logs exceeded…

    Thread Starter biatsalu

    (@biatsalu)

    Apparently, there is still the problem of json syntax error frequently, but it no longer gives curl limit error.

    Any idea what it could be? I have all error_logs disabled, and the file I’m working on has no PHP error.

    The woocommerce logs show the same error I see in the file I am running.

    • This reply was modified 2 years ago by biatsalu.
    Saif

    (@babylon1999)

    Hello @biatsalu,

    According to a user here, changing the permalink settings to default solved the issue which makes sense as it will re-write the rules of the .htaccess file.

    Let us know how it goes!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘API REST Woocommerce – JSON ERROR’ is closed to new replies.