在WooCommerce REST API中获取按ID获取产品详细信息的异常

时间:2020-10-07 作者:Pranav Patel

我需要在woocommerce REST API中创建订单

我尝试了以下代码

require(\'../wp-blog-header.php\'); 

header("HTTP/1.1 200 OK"); 
header("Status: 200 All rosy");

require __DIR__ . \'/vendor/autoload.php\';

use Automattic\\WooCommerce\\Client;

// Local Server
$woocommerce = new Client(
    \'https://exmapledomain.com/\', 
    \'key1\', 
    \'key2\',
    [
        \'wp_api\' => true,
        \'timeout\' => 45000,
        \'version\' => \'wc/v3\',
    ]
);

print_r($woocommerce->get(\'products/803\'));exit;
我得到如下错误

2020-10-07T11:38:40+05:30严重未捕获自动\\WooCommerce\\HttpClient\\HttpClientException:错误:抱歉,您无法查看此资源。在/opt/bitnami/apps/wordpress/htdocs/app/src/woomerce/HttpClient/HttpClient中[Woomerce\\u rest\\u cannot\\u view]。php:324堆栈跟踪:

#0/opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/HttpClient/HttpClient。php(349):Automatic\\WooCommerce\\HttpClient\\HttpClient->;lookForErrors(数组)

#1/opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/HttpClient/HttpClient。php(385):Automatic\\WooCommerce\\HttpClient\\HttpClient->;processResponse()

#2/opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/Client。php(82):Automatic\\WooCommerce\\HttpClient\\HttpClient->;请求(\'products/803\',\'GET\',Array,Array)

#3/opt/bitnami/apps/wordpress/htdocs/app/orderApi。php(38):Automatic\\WooCommerce\\Client->;获取(\'products/803\')

#4{main}抛出/opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/HttpClient/HttpClient。php在线324

技术版本1。PHP版本:7.3.18。wordpress:4.5.2。woocommerce:5.5.1。REST api包:4.5.2

1 个回复
SO网友:Pranav Patel

我已通过在代码中添加以下行来解决该错误。

\'query_string_auth\' => true

code should be like below

 // Local Server
$woocommerce = new Client(
    \'https://exmapledomain.com/\', 
    \'key1\', 
    \'key2\',
    [
        \'wp_api\' => true,
        \'timeout\' => 45000,
        \'version\' => \'wc/v3\',
        \'query_string_auth\' => true,
    ]
);