woocommerce / wc-api-php

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

Invalid JSON returned #10

Closed ad-on-is closed 8 years ago

ad-on-is commented 8 years ago

I would like to list all products, therefore I used the filter[limit] like this

$woocommerce->get('products', array('filter[limit]' => 10000, 'fields' => 'id,title,sku,variations'));

Unfortunately I get PHP-error message saying

Fatal error: Uncaught exception 'Automattic\WooCommerce\HttpClient\HttpClientException' with message 'Invalid JSON returned' in /var/www/html/wcapi/src/WooCommerce/HttpClient/HttpClient.php:303 Stack trace: #0 /var/www/html/wcapi/src/WooCommerce/HttpClient/HttpClient.php(346): Automattic\WooCommerce\HttpClient\HttpClient->lookForErrors(NULL) #1 /var/www/html/wcapi/src/WooCommerce/HttpClient/HttpClient.php(382): Automattic\WooCommerce\HttpClient\HttpClient->processResponse() #2 /var/www/html/wcapi/src/WooCommerce/Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request('products', 'GET', Array, Array) #3 /var/www/html/wcapi/wcapi.php(1189): Automattic\WooCommerce\Client->get('products', Array) #4 {main} thrown in /var/www/html/wcapi/src/WooCommerce/HttpClient/HttpClient.php on line 303

Is there a way to list all products?

claudiosanches commented 8 years ago

Are you using some plugin for cache?

ad-on-is commented 8 years ago

No. W3TotalCache is deactivated on dev-environment. Tried with and without php-opcache.

I have analysed it a bit more. It works when I set the limit to something like 2000 and lower.

claudiosanches commented 8 years ago

@ad-on-is seems like a timeout problem.

ad-on-is commented 8 years ago
$woocommerce = new Client(
    'http://project.local', 
    'ck_xxx', 
    'cs_xxx',
    array(
        'timeout' => 1000
    )
);

this is what my settings look like.

in php.ini I've set: max_execution_time = 0 and max_input_time = -1

claudiosanches commented 8 years ago

Still don't seems an error on this wrapper. The error that you are reporting may appear in when PHP throw any kind of warning. Since will mix HTML and JSON. Have you checked it?

ad-on-is commented 8 years ago

Since will mix HTML and JSON.

I don't actually understand what you mean here. The returning JSON can contain HTML-Content in the product-description which is post_content itself.

claudiosanches commented 8 years ago

PHP warning (HTML) + JSON. Have you enabled WordPress Debug mode?

ad-on-is commented 8 years ago

Debug mode is disabled. I wanted to check $this->response->getBody() what it actually returns before it's sent to lookForErrors() in HttpClient.php on line 344, but I get an empty string there.

claudiosanches commented 8 years ago

@ad-on-is you can use:

use Automattic\WooCommerce\HttpClient\HttpClientException;

try {
    // Your request here!
} catch (HttpClientException $e) {
    \print_r($e);
    \print_r($e->getMessage() . PHP_EOL);
    \print_r('Code: ' . $e->getResponse()->getCode() . PHP_EOL);
    \print_r('Body: ' . $e->getResponse()->getBody() . PHP_EOL);
}

$e->getResponse()->getBody() will return the raw response.

ad-on-is commented 8 years ago

This is what i get:


Automattic\WooCommerce\HttpClient\HttpClientException Object
(
    [request:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Request Object
        (
            [url:Automattic\WooCommerce\HttpClient\Request:private] => http://schumcb.local/wc-api/v3/products?fields=id%2Ctitle%2Csku%2Cvariations&filter%5Blimit%5D=3000&oauth_consumer_key=ck_57ab7d36f5db1a29428e12f2c61e809d1eca5b2d&oauth_nonce=3961c5ca3ba7ebf3d860cb7557b4624b420a6643&oauth_signature=HmyaY0ERUj8MkpdC9gmevbRLjo1dQTH7urUPFL04YkE%3D&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1457943406
            [method:Automattic\WooCommerce\HttpClient\Request:private] => GET
            [parameters:Automattic\WooCommerce\HttpClient\Request:private] => Array
                (
                    [fields] => id,title,sku,variations
                    [filter[limit]] => 3000
                    [oauth_consumer_key] => ck_57ab7d36f5db1a29428e12f2c61e809d1eca5b2d
                    [oauth_nonce] => 3961c5ca3ba7ebf3d860cb7557b4624b420a6643
                    [oauth_signature] => HmyaY0ERUj8MkpdC9gmevbRLjo1dQTH7urUPFL04YkE=
                    [oauth_signature_method] => HMAC-SHA256
                    [oauth_timestamp] => 1457943406
                )

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

            [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, 14 Mar 2016 08:16:46 GMT
                    [Server] => Apache/2.4.7 (Ubuntu)
                    [X-Powered-By] => PHP/5.5.9-1ubuntu4.14
                    [Set-Cookie] => wp_woocommerce_session_b5362922f3ed97c0e7310ac8aa4cafa2=68f474ee8fe8eed49065883fd51c30bc%7C%7C1458116211%7C%7C1458112611%7C%7Cf60451486fc63e0bb6a16813ff70b12f; expires=Wed, 16-Mar-2016 08:16:51 GMT; Max-Age=172799; path=/
                    [Expires] => Thu, 19 Nov 1981 08:52:00 GMT
                    [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
                    [Pragma] => no-cache
                    [Content-Length] => 0
                    [Content-Type] => application/json; charset=UTF-8
                )

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

    [message:protected] => Invalid JSON returned
    [string:Exception:private] => 
    [code:protected] => 200
    [file:protected] => /var/www/html/wcapi/src/WooCommerce/HttpClient/HttpClient.php
    [line:protected] => 303
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /var/www/html/wcapi/src/WooCommerce/HttpClient/HttpClient.php
                    [line] => 346
                    [function] => lookForErrors
                    [class] => Automattic\WooCommerce\HttpClient\HttpClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => 
                        )

                )

            [1] => Array
                (
                    [file] => /var/www/html/wcapi/src/WooCommerce/HttpClient/HttpClient.php
                    [line] => 382
                    [function] => processResponse
                    [class] => Automattic\WooCommerce\HttpClient\HttpClient
                    [type] => ->
                    [args] => Array
                        (
                        )

                )

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

                            [3] => Array
                                (
                                    [filter[limit]] => 3000
                                    [fields] => id,title,sku,variations
                                )

                        )

                )

            [3] => Array
                (
                    [file] => /var/www/html/wcapi/wcapi2.php
                    [line] => 30
                    [function] => get
                    [class] => Automattic\WooCommerce\Client
                    [type] => ->
                    [args] => Array
                        (
                            [0] => products
                            [1] => Array
                                (
                                    [filter[limit]] => 3000
                                    [fields] => id,title,sku,variations
                                )

                        )

                )

        )

    [previous:Exception:private] => 
)
Invalid JSON returned
Code: 200
Body: 
ad-on-is commented 8 years ago

Well, updating from woocommerce 2.5.2 to 2.5.5 solved the issue

claudiosanches commented 8 years ago

For some reason is returning empty, probably because of an error... But really not an issue in this wrapper.

apuestaya commented 8 years ago

Hi, @ad-on-is, did you found solution for this issue? Thank you!

azizchouhan commented 8 years ago

Well, when you will change your permalink settings , Plain to Post name, this error will disappear.

so change your site's permalink setting, dont use default setting

Lavkushwaha commented 7 years ago

i am getting results but seems not like in Proper JSON format;

Array ( [0] => Array ( [id] => 15 [name] => Logo T-shirt [slug] => logo-t-shirt [permalink] => http://incals.in/product/logo-t-shirt/ [date_created] => 2016-11-23T11:28:27 [date_modified] => 2016-11-23T11:28:28 [type] => simple [status] => publish [featured] => [catalog_visibility] => visible [description] => This is a sample product description.

[short_description] => This is a short description.

[sku] => [price] => 18 [regular_price] => 20 [sale_price] => 18 [date_on_sale_from] => [date_on_sale_to] => [price_html] => $20.00 $18.00 [on_sale] => 1 [purchasable] => 1 [total_sales] => 0 [virtual] => [downloadable] => [downloads] => Array ( ) [download_limit] => -1 [download_expiry] => -1 [download_type] => standard [external_url] => [button_text] => [tax_status] => taxable [tax_class] => [manage_stock] => 1 [stock_quantity] => 5 [in_stock] => 1 [backorders] => no [backorders_allowed] => [backordered] => [sold_individually] => [weight] => [dimensions] => Array ( [length] => [width] => [height] => ) [shipping_required] => 1 [shipping_taxable] => 1 [shipping_class] => [shipping_class_id] => 0 [reviews_allowed] => 1 [average_rating] => 4.00 [rating_count] => 1 [related_ids] => Array ( [0] => 50 [1] => 37 [2] => 34 ) [upsell_ids] => Array ( [0] => 60 ) [cross_sell_ids] => Array ( ) [parent_id] => 0 [purchase_note] => [categories] => Array ( [0] => Array ( [id] => 9 [name] => Clothing [slug] => clothing ) [1] => Array ( [id] => 14 [name] => T-shirts [slug] => t-shirts ) ) [tags] => Array ( ) [images] => Array ( [0] => Array ( [id] => 172 [date_created] => 2016-11-23T11:28:27 [date_modified] => 2016-11-23T11:28:28 [src] => http://incals.in/wp-content/uploads/2016/11/logo_tee_front.jpg [name] => Logo T-shirt [alt] => [position] => 0 ) [1] => Array ( [id] => 172 [date_created] => 2016-11-23T11:28:27 [date_modified] => 2016-11-23T11:28:28 [src] => http://incals.in/wp-content/uploads/2016/11/logo_tee_front.jpg [name] => Logo T-shirt [alt] => [position] => 1 ) [2] => Array ( [id] => 173 [date_created] => 2016-11-23T11:28:27 [date_modified] => 2016-11-23T11:28:28 [src] => http://incals.in/wp-content/uploads/2016/11/logo_tee_back.jpg [name] => Logo T-shirt [alt] => [position] => 2 ) ) [attributes] => Array ( ) [default_attributes] => Array ( ) [variations] => Array ( ) [grouped_products] => Array ( ) [menu_order] => 0 [_links] => Array ( [self] => Array ( [0] => Array ( [href] => http://incals.in/wp-json/wc/v1/products/15 ) ) [collection] => Array ( [0] => Array ( [href] => http://incals.in/wp-json/wc/v1/products ) ) ) ) [1] => Array ( [id] => 34 [name] => Ninja T-shirt [slug] => ninja-t-shirt [permalink] => http://incals.in/product/ninja-t-shirt/ [date_created] => 2016-11-23T11:28:27 [date_modified] => 2016-11-23T11:28:28 [type] => simple [status] => publish [featured] => [catalog_visibility] => visible [description] => This is a sample product description.

[short_description] => This is a short description. . . .

what kind of JSON it is ? even i have tried these too http://json-schema.org/example1.html www.jsoneditoronline.org/ & got nothing but ERRORS

ref website try yourself http://incals.in/api

animatedcreativity commented 4 years ago

Well, when you will change your permalink settings , Plain to Post name, this error will disappear.

so change your site's permalink setting, dont use default setting

Solved the problem for me. šŸ’ƒ You solved my other day. Wasted one already. šŸ‘

craftedsro commented 4 years ago

In my case i have to disable page cache in plugin Total Cache -> General settings -> Page cache.