0

still trying to get my head around API's and connections.

I am trying to do a buy order to Binance but keep getting a 400 response. I know it's been an issue for some others but I just can't seem to suss out where I am going wrong. So hoping someone can help me out.

It's only issues with POST's, all the GET requests are working fine, and so to the signature and timestamp... or at least I am getting responses for my account so I assume so.

The first CURL is just for the serverTime, but the second CURL is for a buy/sell order.

This is the response I am getting now...

"Object ( [code] => -1102 [msg] => Mandatory parameter 'side' was not sent, was empty/null, or malformed. )"

If I type the in the string manually it works just fine, but for one reason or another when I pass $qs it presents the above fault. I echoed $qs to the screen and copied that instead of passing $qs and it worked when I put in the new timestamp. I am stumped...

"symbol=TRXUSDC&side=SELL&type=LIMIT&timeInForce=GTC&quantity=63000.00000000&price=0.02550000&recvWindow=1000000&timestamp=1550922237310"

any advice?

$header = array('X-MBX-APIKEY:' . KEY);
$url = BINANCE . 'api/v1/time';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_URL,$url);
$response = curl_exec($curl);

if (FALSE === $response){
        echo curl_error($curl), curl_errno($curl);
    }
$serverTime = json_decode($response);
curl_close($curl);

$url = BINANCE . "api/v3/order";
$signature = NULL;
$queryString = NULL;

$query = array(
    "symbol" => "TRXUSDC",
    "side" => "SELL",
    "type" => "LIMIT",
    "timeInForce" => "GTC", 
    "quantity" => number_format(63000.00000000,8,'.',''),
    "price" => number_format(0.02550000,8,'.',''), 
    "recvWindow" => 1000000,
    "timestamp" => $serverTime->serverTime);

$qs = htmlentities(http_build_query(array_unique($query)));
$query['signature'] = hash_hmac('SHA256', $qs, SECRET );
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_ENCODING, "application/x-www-form-urlencoded");
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl,CURLOPT_FAILONERROR,FALSE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$qs);
curl_setopt($curl, CURLOPT_URL, $url);
$response = curl_exec($curl);
if (FALSE === $response){
    echo curl_error($curl).':'.curl_errno($curl);
}
$obj = json_decode($response);
curl_close($curl);

3 Answers3

0

I tried this code and var_dump $qs

symbol=BTCUSDT&side=BUY&...

so I think you must remove amp;

GandhyOnly
  • 325
  • 2
  • 5
  • 18
0

Based on Binance spot api docs:

It is recommended to use a small recvWindow of 5000 or less! The max cannot go beyond 60,000!

so edit your query params with:

"symbol=TRXUSDC&side=SELL&type=LIMIT&timeInForce=GTC&quantity=63000.00000000&price=0.02550000&recvWindow=60000&timestamp=1550922237310"

Also you can check your other query params with exchange info that you can get with https://api.binance.com/api/v3/exchangeInfo that we have for your symbol:

{
  "symbol": "TRXUSDC",
  "status": "TRADING",
  "baseAsset": "TRX",
  "baseAssetPrecision": 8,
  "quoteAsset": "USDC",
  "quotePrecision": 8,
  "quoteAssetPrecision": 8,
  "baseCommissionPrecision": 8,
  "quoteCommissionPrecision": 8,
  "orderTypes": [
    "LIMIT",
    "LIMIT_MAKER",
    "MARKET",
    "STOP_LOSS_LIMIT",
    "TAKE_PROFIT_LIMIT"
  ],
  "icebergAllowed": true,
  "ocoAllowed": true,
  "quoteOrderQtyMarketAllowed": true,
  "isSpotTradingAllowed": true,
  "isMarginTradingAllowed": false,
  "filters": [
    {
      "filterType": "PRICE_FILTER",
      "minPrice": "0.00001000",
      "maxPrice": "1000.00000000",
      "tickSize": "0.00001000"
    },
    {
      "filterType": "PERCENT_PRICE",
      "multiplierUp": "5",
      "multiplierDown": "0.2",
      "avgPriceMins": 5
    },
    {
      "filterType": "LOT_SIZE",
      "minQty": "0.10000000",
      "maxQty": "9000000.00000000",
      "stepSize": "0.10000000"
    },
    {
      "filterType": "MIN_NOTIONAL",
      "minNotional": "10.00000000",
      "applyToMarket": true,
      "avgPriceMins": 5
    },
    {
      "filterType": "ICEBERG_PARTS",
      "limit": 10
    },
    {
      "filterType": "MARKET_LOT_SIZE",
      "minQty": "0.00000000",
      "maxQty": "659177.02430556",
      "stepSize": "0.00000000"
    },
    {
      "filterType": "MAX_NUM_ORDERS",
      "maxNumOrders": 200
    },
    {
      "filterType": "MAX_NUM_ALGO_ORDERS",
      "maxNumAlgoOrders": 5
    }
  ],
  "permissions": [
    "SPOT"
  ]
}

for other possible reasons check spot api docs in the link https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md

0

I tested the code and saw a few glitches in it :

$qs = htmlentities(http_build_query(array_unique($query)));

gave me an error ({"code":-1102,"msg":"Mandatory parameter 'side' was not sent, was empty/null, or malformed."}), so i replaced it with:

$qs = http_build_query(array_unique($query));

After that you created your signature, but never added it to you request. So do the following :

$signature = hash_hmac('sha256', $qs, $apiSecret);
$qs .= "&signature=" . $signature;

Also update your 'recvWindow' to a max of 60000 (I suggest 50000 or less (see the answer of @Tofiq Samali)) and you are ready to go!