首页 > 解决方案 > WC API:返回无效的 JSON

问题描述

我在这里做错了什么?!自从我杀死 Google 和 stackoverflow 已经两天了......我想不通:(((

我已经在线检查了返回的正文 json(由于它很长,所以没有将其粘贴在这里)并且是有效的 json ......问题出在哪里?!

没有安装缓存插件,邮递员中的相同请求返回OK结果,直接在浏览器中测试请求URL返回OK结果,一切都很好,除非我在我的项目中使用......

Automattic\WooCommerce\HttpClient\HttpClientException 对象([request:Automattic\WooCommerce\HttpClient\HttpClientException:private] => Automattic\WooCommerce\HttpClient\Request 对象([url:Automattic\WooCommerce\HttpClient\Request:private] => https: //www.viatainrawz.ro/wp-json/wc/v3/products [方法:Automattic\WooCommerce\HttpClient\Request:private] => GET [参数:Automattic\WooCommerce\HttpClient\Request:private] => 数组 ( )

        [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, 31 Aug 2020 09:26:42 GMT
                [content-type] => application/json; charset=UTF-8
                [vary] => Accept-Encoding
                [x-robots-tag] => noindex
                [x-content-type-options] => nosniff
                [access-control-expose-headers] => X-WP-Total, X-WP-TotalPages, Link
                [access-control-allow-headers] => Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type
                [expires] => Wed, 11 Jan 1984 05:00:00 GMT
                [cache-control] => no-cache, must-revalidate, max-age=0
                [x-wp-total] => 49
                [x-wp-totalpages] => 5
                [link] => <https://viatainrawz.ro/wp-json/wc/v3/products?page=2>; rel="next"
                [allow] => GET, POST
                [server] => ClausWeb-nginx
                [cluster-host] => server46.romania-webhosting.com
            )

        
    )

[message:protected] => Invalid JSON returned
[string:Exception:private] => 
[code:protected] => 200
[file:protected] => /home/vreaumob/worker.vreaumobila.ro/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
[line:protected] => 303
[trace:Exception:private] => Array
    (
        [0] => Array
            (
                [file] => /home/vreaumob/worker.vreaumobila.ro/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
                [line] => 346
                [function] => lookForErrors
                [class] => Automattic\WooCommerce\HttpClient\HttpClient
                [type] => ->
                [args] => Array
                    (
                        [0] => 
                    )

            )

        [1] => Array
            (
                [file] => /home/vreaumob/worker.vreaumobila.ro/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php
                [line] => 382
                [function] => processResponse
                [class] => Automattic\WooCommerce\HttpClient\HttpClient
                [type] => ->
                [args] => Array
                    (
                    )

            )

        [2] => Array
            (
                [file] => /home/vreaumob/worker.vreaumobila.ro/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
                            (
                            )

                    )

            )

        [3] => Array
            (
                [file] => /home/vreaumob/worker.vreaumobila.ro/apitest.php
                [line] => 24
                [function] => get
                [class] => Automattic\WooCommerce\Client
                [type] => ->
                [args] => Array
                    (
                        [0] => products
                    )

            )

    )

[previous:Exception:private] => 

) 返回无效的 JSON

标签: jsonapiwc

解决方案


我使用的代码:

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
try {
  $woocommerce = new Client( 'https://www.viatainrawz.ro','XX', 'XX',
        [
            'verify_ssl' => false,
            'version' => 'wc/v3'
        ]
    );
   print_r($woocommerce->get('products'));

} 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);
}

推荐阅读