我想进行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] =&gt;数组([0] =&gt;接受: application / json 1 =&gt; Content-Type:application / json [2] =&gt; User-Agent:WooCommerce API Client-PHP / 2.0.1)[method] =&gt; POST [url] =&GT; 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] =&gt;数组([oauth_consumer_key] =&gt; ck_0cfc7bc73277efd3eb665b52234ae8939b39cb0a [oauth_timestamp] =&gt; 1477892703 [oauth_nonce] =&gt; 08e418dcf02c304ccfab4d09ed3233074acc4f11 [oauth_signature_method] =&gt; HMAC-SHA256 [oauth_signature] =&gt; HqW4ra / 3EPhnByREOQjG9VybB2FjSpDJhC0PVVSnUZ8 =)[data] =&gt; [body] =&gt; null [持续时间] =&gt; 1.14179)stdClass对象([body] =&gt; { “错误”:[{ “代码”: “woocommerce_api_missing_callback_param”, “消息”:“缺少 参数数据“}]} [code] =&gt; 400 [headers] =&gt;数组([Date] =&gt; Mon, 2016年10月31日05:45:04 GMT [服务器] =&gt; Apache / 2.2.27(Unix) mod_ssl / 2.2.27 OpenSSL / 1.0.1e-fips [X-Powered-By] =&gt; PHP / 5.6.26 [Vary] =&GT; Accept-Encoding [Connection] =&gt;关闭[Transfer-Encoding] =&gt; chunked [Content-Type] =&gt;应用/ JSON; charset = UTF-8))
有人能帮助我吗?我该怎么办 ?
答案 0 :(得分:1)
我们可以通过将woocommerce_api_missing_callback_param
请求替换为PUT
请求来更新订单时解决奇怪的POST
错误。
在我们的例子中,服务器上的某些东西似乎阻止了PUT参数。
答案 1 :(得分:0)
从代码中你没有定义$ data数组,它应该捕获数据类型。根据您正在使用的api库尝试:
print_r( $client
->products
->create( array(
'title' => 'Test Product',
'type' => 'simple',
'regular_price' => '9.99',
'description' => 'test' ) )
);