0

仍在尝试了解 API 和连接。

我正在尝试向 Binance 下订单,但一直收到 400 响应。我知道这对其他一些人来说是个问题,但我似乎无法确定我哪里出错了。所以希望有人可以帮助我。

这只是 POST 的问题,所有 GET 请求都工作正常,签名和时间戳也是如此……或者至少我收到了我的帐户的响应,所以我假设是这样。

第一个 CURL 仅用于 serverTime,但第二个 CURL 用于买入/卖出订单。

这是我现在得到的回应......

“对象([code] => -1102 [msg] => 未发送强制参数'side',为空/null,或格式错误。)”

如果我手动在字符串中键入它就可以了,但是由于某种原因,当我通过 $qs 时,它会出现上述错误。我在屏幕上回显了 $qs 并复制了它而不是传递 $qs 并且当我输入新的时间戳时它起作用了。我被难住了……

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

有什么建议吗?

$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);
4

2 回答 2

0

我试过这段代码和 var_dump $qs

symbol=BTCUSDT&side=BUY&...

所以我认为你必须删除 amp;

于 2019-03-10T09:55:03.237 回答
0

基于币安现货 api 文档:

建议使用5000以下的小recvWindow!最大值不能超过60,000!

所以编辑你的查询参数:

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

您还可以使用我们为您的符号提供的https://api.binance.com/api/v3/exchangeInfo获得的交换信息检查您的其他查询参数:

{
  "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"
  ]
}

出于其他可能的原因,请检查链接中的现场 api 文档https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md

于 2021-04-16T21:24:40.747 回答