woocommerce / wc-api-php

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

JSON ERROR: Syntax error in C:\laragon\www\contactform7\HttpClient\HttpClient.php:378 #260

Open peterb776 opened 4 years ago

peterb776 commented 4 years ago

I am trying to get the API working on my local development (laptop) server using Laragon 4 (PHP 7.2), setting up the installation without using composer as on the target system I only have limited CPanel access.

The host system is running WordPress 5.2.5 and WooCommerce 3.5.7, with the REST API enabled.

Using the following PHP code (copied from the API documentation, plus a suggested mod to deal with Laragon's self-signed certificates) as the stand-alone index.php on another domain:


<html>
<head>
<title>Test of WooC REST API for creating products</title>
</head>
<body>
<?php
require __DIR__ . '/HttpClient/BasicAuth.php';
require __DIR__ . '/HttpClient/HttpClient.php';
require __DIR__ . '/HttpClient/HttpClientException.php';
require __DIR__ . '/HttpClient/OAuth.php';
require __DIR__ . '/HttpClient/Options.php';
require __DIR__ . '/HttpClient/Request.php';
require __DIR__ . '/HttpClient/Response.php';

require __DIR__ . '/Client.php';

use Automattic\WooCommerce\Client;

try{
    $woocommerce = new Client(
        'http://artstrailgallerx.local',
        'ck_********a51973838e1f453036d292b3b1da327',
        'cs_********54b5c08c32e79a808fdc523a4429428c',
        [
            'wp_api'  => true,
            'version' => 'wc/v3',
            'query_string_auth' => true, // Deal with Laragon's
            'verify_ssl' => false,              // self-signed certificates
        ]
    );
    echo '<p>Created API client link.</p>';
}
catch (HttpClientException $e) {
    echo '<pre><code>' . print_r( $e->getMessage(), true ) . '</code><pre>'; // Error message.
    echo '<pre><code>' . print_r( $e->getRequest(), true ) . '</code><pre>'; // Last request data.
    echo '<pre><code>' . print_r( $e->getResponse(), true ) . '</code><pre>'; // Last response data.
}
flush();

try {
print_r($woocommerce->get('products/categories'));
}
catch (HttpClientException $e) {
    echo '<pre><code>' . print_r( $e->getMessage(), true ) . '</code><pre>'; // Error message.
    echo '<pre><code>' . print_r( $e->getRequest(), true ) . '</code><pre>'; // Last request data.
    echo '<pre><code>' . print_r( $e->getResponse(), true ) . '</code><pre>'; // Last response data.
}
flush();
?>
</body>
</html>

All I get as output is the following, with success(?) on creating the API connection but then a critical error:


Created API client link.

Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: JSON ERROR: Syntax error in C:\laragon\www\contactform7\HttpClient\HttpClient.php:378 Stack trace: #0 C:\laragon\www\contactform7\HttpClient\HttpClient.php(422): Automattic\WooCommerce\HttpClient\HttpClient->processResponse() #1 C:\laragon\www\contactform7\Client.php(56): Automattic\WooCommerce\HttpClient\HttpClient->request('products', 'POST', Array) #2 C:\laragon\www\contactform7\index2.php(58): Automattic\WooCommerce\Client->post('products', Array) #3 {main} thrown in C:\laragon\www\contactform7\HttpClient\HttpClient.php on line 378

Is there something else that I should be loading? If not, how can I debug this (I'm not a PHP expert)?

Thanks, Peter

Er-Kalpesh commented 4 years ago

any solution for the above issue? I am facing same issue

peterb776 commented 4 years ago

I've had no comments or suggestions, so I've made no progress. As my client needs something 'today' I've had to resort to using the WooCommerce CSV importer just to have something that works even if not very well.

I'm not happy to try and debug the API code, but might try it again sometime if there is a newer version.