需要帮助和信息有关Woocommerce创建订单API的提示

问题描述 投票:2回答:2

我想进行API调用。我正在使用WordPress的Woocommerce插件。我正在尝试使用kloon/WooCommerce-REST-API-Client-Library设置API

所以我使用基本的api命令来创建一个订单

我的代码:

<?php

require_once( '../lib/woocommerce-api.php' );

$consumer_key = 'ck_0cfc7bc73277efd3eb665b52234ae8939b39cb0a'; // Add your own Consumer Key here
$consumer_secret = 'cs_ef229872c4620c46d1b71b52537b3279e0e9dcdb'; // Add your own Consumer Secret here
$store_url = 'http://example.net'; // Add the home URL to the store you want to connect to here

$options = array(
    'debug'           => true,
    'return_as_array' => false,
    'validate_url'    => false,
    'timeout'         => 30,
    'ssl_verify'      => false,
);

try {

    $client = new WC_API_Client( $store_url, $consumer_key, $consumer_secret, $options );

    print_r( $client->orders->create( $data ) );

} catch ( WC_API_Client_Exception $e ) {

    echo $e->getMessage() . PHP_EOL;
    echo $e->getCode() . PHP_EOL;

    if ( $e instanceof WC_API_Client_HTTP_Exception ) {

        print_r( $e->get_request() );
        print_r( $e->get_response() );
    }
}

我的错误:

错误:缺少参数数据[woocommerce_api_missing_callback_param] 400 stdClass对象([headers] =>数组([0] =>接受:application / json 1 => Content-Type:application / json [2] => User-Agent:WooCommerce API Client-PHP / 2.0.1)[method] => POST [url] => http://example.net/test/wc-api/v2/orders?oauth_consumer_key=ck_0cfc7bc73277efd3eb665b52234ae8939b39cb0a&oauth_timestamp=1477892703&oauth_nonce=08e418dcf02c304ccfab4d09ed3233074acc4f11&oauth_signature_method=HMAC-SHA256&oauth_signature=HqW4ra%2F3EPhnByREOQjG9VybB2FjSpDJhC0PVVSnUZ8%3D [params] =>数组([oauth_consumer_key] => ck_0cfc7bc73277efd3eb665b52234ae8939b39cb0a [oauth_timestamp] => 1477892703 [oauth_nonce] => 08e418dcf02c304ccfab4d09ed3233074acc4f11 [oauth_signature_method] => HMAC-SHA256 [oauth_signature] => HqW4ra / 3EPhnByREOQjG9VybB2FjSpDJhC0PVVSnUZ8 =)[data] => [body] => null [duration] => 1.14179)stdClass对象([body] => {“errors”:[{“code”: “woocommerce_api_missing_callback_param”,“message”:“缺少参数数据”}]} [code] => 400 [headers] =>数组([日期] =>星期一,2016年10月31日05:45:04 GMT [服务器] => Apache / 2.2.27(Unix)mod_ssl / 2.2.27 OpenSSL / 1.0.1e-fips [X-Powered-By] => PHP / 5.6.26 [Vary] => Accept-Encoding [Connection] => close [转移] -Encoding] => chunked [内容-Type] => application / json; charset = UTF-8))

有人能帮我吗 ?我该怎么办 ?

php wordpress api woocommerce
2个回答
1
投票

我们能够通过用woocommerce_api_missing_callback_param请求替换PUT请求来更新订单时解决奇怪的POST错误。在我们的例子中,服务器上的某些东西似乎阻止了PUT参数。


0
投票

从代码中你没有定义$ data数组与它应该捕获的数据类型。根据您正在使用的api库尝试:

print_r( $client
          ->products
          ->create( array( 
            'title' => 'Test Product', 
            'type' => 'simple', 
            'regular_price' => '9.99', 
            'description' => 'test' ) ) 
);
© www.soinside.com 2019 - 2024. All rights reserved.