Invalid command error on poloniex using laravel Http facade

75 views Asked by At

Switching from curl implementation to using laravel Http facade to access poloniex private api.

But I am having issues with it. I get an invalid command response from poloniex, yet all my parameters seem to work alright.

Sample code below, this is the case with all endpoints but we'll use the returnBalances command to test this here:

$req = ['command' => 'returnBalances'];

// generate a nonce
$time = explode(' ', microtime());
$req['nonce'] = $time[1].substr($time[0], 2, 6);

$parameters = http_build_query($req, '', '&');

$sign = hash_hmac('sha512', $parameters, $my_secret);

$response = Http::withHeaders(["key" => $my_key, 'Sign' '=> $my_secret])
    ->post('https://poloniex.com/tradingApi', $req);

The above response json returns:

array:1 [
  "error" => "Invalid command."
].

My code works fine though when working directly with curl, seems like the http facade is not sending request parameters

1

There are 1 answers

0
James On

It looks like you're sending your request payload as query parameters.

Reviewing the docs, you need to add in a call to asForm() before your post() call.

The below, assuming the rest of your code is correct, should be the correct implementation:

$response = Http::withHeaders(["key" => $my_key, 'Sign' => $my_secret])
    ->asForm()
    ->post('https://poloniex.com/tradingApi', ['command' => 'returnBalances']);

I believe the Http client takes care of URL encoding for you, so you don't need the extra call to http_build_query.