wc-api-php icon indicating copy to clipboard operation
wc-api-php copied to clipboard

JSON ERROR: Syntax error

Open iw0100 opened this issue 3 years ago • 8 comments

I tried get product using API call but i got below error i check this and many other blog. Also i changes as per recommendation on the server as per suggestion but I didn't find the solution for this error :- JSON ERROR: Syntax error I am Using 3.0.0 Version Working Fine in Postman

	Automattic\WooCommerce\HttpClient\HttpClientException Object
	(
	    [request:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Request Object
	        (
	            [url:Automattic\WooCommerce\HttpClient\Request:private] => https://www.website.com/wp-json/wc/v3/products?status=publish
	            [method:Automattic\WooCommerce\HttpClient\Request:private] => GET
	            [parameters:Automattic\WooCommerce\HttpClient\Request:private] => Array
	                (
	                    [status] => publish
	                    [per_page] => 1
	                    [stock_status] => instock
	                    [orderby] => date
	                )

	            [headers:Automattic\WooCommerce\HttpClient\Request:private] => Array
	                (
	                    [Accept] => application/json
	                    [User-Agent] => WooCommerce API Client-PHP/3.0.0
	                )

	            [body:Automattic\WooCommerce\HttpClient\Request:private] => 
	        )

	    [response:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Response Object
	        (
	            [code:Automattic\WooCommerce\HttpClient\Response:private] => 200
	            [headers:Automattic\WooCommerce\HttpClient\Response:private] => Array
	                (
	                    [Date] => Mon, 06 Jun 2022 12:56:00 GMT
	                    [Content-Type] => text/html
	                    [Transfer-Encoding] => chunked
	                    [Connection] => keep-alive
	                    [last-modified] => Monday, 06-Jun-2022 12:56:00 GMT
	                    [Cache-Control] => private, no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0, s-maxage=0
	                    [cf-edge-cache] => no-cache
	                    [CF-Cache-Status] => DYNAMIC
	                    [Expect-CT] => max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
	                    [Report-To] => {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=PlYSeCqCLScJndCR5WtftcnAH3Vi2twyTa6oaQ7b6t31U2CSDoHoxTDJLJs%2FvQhHiNOmoZLZWlo2lw9m%2Fe9HoyHbilpzHupYr4mArCv74VqKNA%2BfNNhZyA1QATtUCJx7FMiRdaV6508DkH0zVA%3D%3D"}],"group":"cf-nel","max_age":604800}
	                    [NEL] => {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
	                    [Server] => cloudflare
	                    [CF-RAY] => 71714e3bdae06368-ORD
	                    [alt-svc] => h3=":443"; ma=86400, h3-29=":443"; ma=86400
	                )

	            [body:Automattic\WooCommerce\HttpClient\Response:private] =>

	        )

	    [message:protected] => JSON ERROR: Syntax error
	    [string:Exception:private] => 
	    [code:protected] => 200
	    [file:protected] => /var/www/html/sidecart/woocommerce/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
	    [line:protected] => 378
	    [trace:Exception:private] => Array
	        (
	            [0] => Array
	                (
	                    [file] => /var/www/html/sidecart/woocommerce/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
	                    [line] => 422
	                    [function] => processResponse
	                    [class] => Automattic\WooCommerce\HttpClient\HttpClient
	                    [type] => ->
	                    [args] => Array
	                        (
	                        )

	                )

	            [1] => Array
	                (
	                    [file] => /var/www/html/sidecart/woocommerce/vendor/automattic/woocommerce/src/WooCommerce/Client.php
	                    [line] => 82
	                    [function] => request
	                    [class] => Automattic\WooCommerce\HttpClient\HttpClient
	                    [type] => ->
	                    [args] => Array
	                        (
	                            [0] => products
	                            [1] => GET
	                            [2] => Array
	                                (
	                                )

	                            [3] => Array
	                                (
	                                    [status] => publish
	                                    [per_page] => 1
	                                    [stock_status] => instock
	                                    [orderby] => date
	                                )

	                        )

	                )

	            [2] => Array
	                (
	                    [file] => /var/www/html/sidecart/woocommerce.php
	                    [line] => 19
	                    [function] => get
	                    [class] => Automattic\WooCommerce\Client
	                    [type] => ->
	                    [args] => Array
	                        (
	                            [0] => products
	                            [1] => Array
	                                (
	                                    [status] => publish
	                                    [per_page] => 1
	                                    [stock_status] => instock
	                                    [orderby] => date
	                                )

	                        )

	                )

	            [3] => Array
	                (
	                    [file] => /var/www/html/sidecart/woocommerce.php
	                    [line] => 710
	                    [function] => woocommerce_call
	                    [args] => Array
	                        (
	                            [0] => www.website.com
	                            [1] => "consumer_key"
	                            [2] => "consumer_secret"
	                            [3] => products
	                            [4] => GET
	                            [5] => Array
	                                (
	                                    [status] => publish
	                                    [per_page] => 1
	                                    [stock_status] => instock
	                                )

	                        )

	                )

	            [4] => Array
	                (
	                    [file] => /var/www/html/sidecart/woocommerce.php
	                    [line] => 60
	                    [function] => get_graphql_api_list
	                    [class] => Base_Function
	                    [type] => ->
	                    [args] => Array
	                        (
	                            [0] => products
	                            [1] => Array
	                                (
	                                    [status] => publish
	                                    [per_page] => 1
	                                    [stock_status] => instock
	                                    [orderby] => date
	                                )

	                        )

	                )

	        )

	    [previous:Exception:private] => 
	)

iw0100 avatar Jun 07 '22 12:06 iw0100

I'm getting the same error. Although when I user URL with ck & cs as a query string I get all the records

$woocommerce = new Client( 'http://retherun.test/', '***HIDDEN***', '***HIDDEN***', [ 'wp_api' => true, 'version' => 'wc/v3', 'verify_ssl' => false ] );

Here is the error from the client. I'm using it on local env and I have SSL. `JSON ERROR: Syntax error Automattic\WooCommerce\HttpClient\Request Object ( [url:Automattic\WooCommerce\HttpClient\Request:private] => http://retherun.test/wp-json/wc/v3/products/attributes?oauth_consumer_key=HIDDEN&oauth_nonce=HIDDEN&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1654672428&signature=HIDDEN [method:Automattic\WooCommerce\HttpClient\Request:private] => GET [parameters:Automattic\WooCommerce\HttpClient\Request:private] => Array ( [oauth_consumer_key] => HIDDEN [oauth_nonce] => HIDDEN [oauth_signature_method] => HMAC-SHA256 [oauth_timestamp] => 1654672428 [signature] => HIDDEN )

[headers:Automattic\WooCommerce\HttpClient\Request:private] => Array
    (
        [Accept] => application/json
        [User-Agent] => WooCommerce API Client-PHP/3.1.0
    )

[body:Automattic\WooCommerce\HttpClient\Request:private] => 

) Automattic\WooCommerce\HttpClient\Response Object ( [code:Automattic\WooCommerce\HttpClient\Response:private] => 301 [headers:Automattic\WooCommerce\HttpClient\Response:private] => Array ( [Server] => nginx [Date] => Wed, 08 Jun 2022 07:13:48 GMT [Content-Type] => text/html [Content-Length] => 162 [Connection] => keep-alive [Location] => https://retherun.test/wp-json/wc/v3/products/attributes?oauth_consumer_key=ck_HIDDEN&oauth_nonce=HIDDEN&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1654672428&signature=HIDDEN )

[body:Automattic\WooCommerce\HttpClient\Response:private] => 

301 Moved Permanently

nginx

)`

tomektomczuk avatar Jun 08 '22 07:06 tomektomczuk

Have you tried adding 'follow_redirects'?

$options = [
            'wp_api'           => true,
            'version'          => 'wc/v3',
            'verify_ssl'       => false,
            'timeout'          => 180,
            'follow_redirects' => true,
        ];

$client = new Client($url, $consumerKey, $consumerSecret, $options);

WillieHuizing avatar Oct 18 '22 13:10 WillieHuizing

Hello @WillieHuizing. I have tried but I face the same issue.

iw0100 avatar Nov 02 '22 05:11 iw0100

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

StanMenten99 avatar Nov 15 '22 15:11 StanMenten99

Hi Everyone!

I have almost 300 products that needs to exported and all of them are having images, the images are in high resolution which means each image is almost 5 or more then 5MBs. The error is very weird and it is so long I am trying to fix this but nothing helpful material founded yet.

When I export products without images, it works fine and products imported successfully in the WP.

Screenshot from 2023-01-31 19-28-34

umairtipu avatar Jan 31 '23 14:01 umairtipu

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

This worked for me but had to remove the 'www'. So it looks like a configuration issue on the woocommerce server's end

jigzstar avatar Apr 06 '23 14:04 jigzstar

Have you tried adding 'follow_redirects'?

$options = [
            'wp_api'           => true,
            'version'          => 'wc/v3',
            'verify_ssl'       => false,
            'timeout'          => 180,
            'follow_redirects' => true,
        ];

$client = new Client($url, $consumerKey, $consumerSecret, $options);

this one worked for me.

DoctorGuidance avatar Oct 27 '23 11:10 DoctorGuidance