2
votes

I want to make an API call. I'm using the Woocommerce plugin for WordPress. I am trying to setup the APIs using kloon/WooCommerce-REST-API-Client-Library

So I'm use the basic api command to create an order

my code :

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

my error :

Error: Missing parameter data [woocommerce_api_missing_callback_param] 400 stdClass Object ( [headers] => Array ( [0] => Accept: 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] => Array ( [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 Object ( [body] => {"errors":[{"code":"woocommerce_api_missing_callback_param","message":"Missing parameter data"}]} [code] => 400 [headers] => Array ( [Date] => Mon, 31 Oct 2016 05:45:04 GMT [Server] => 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 [Transfer-Encoding] => chunked [Content-Type] => application/json; charset=UTF-8 ) )

Can someone help me ? What should I do ?

2

2 Answers

1
votes

We we able to resolve the strange woocommerce_api_missing_callback_param error when updating an Order by replacing the PUT request with a POST request. In our case, something on the server seemed to block PUT parameters.

0
votes

From the code you havent defined the $ data array with type of data it should capture. According to the api library you are using try:

print_r( $client
          ->products
          ->create( array( 
            'title' => 'Test Product', 
            'type' => 'simple', 
            'regular_price' => '9.99', 
            'description' => 'test' ) ) 
);