php Binance 400响应和检测到的参数的重复值

piztneat  于 2023-01-01  发布在  PHP
关注(0)|答案(3)|浏览(144)

我还在努力理解API和连接。
我正尝试向Binance发出一个购买订单,但一直得到400的回复。我知道这对其他人来说是个问题,但我似乎无法找出我哪里出错了。所以希望有人能帮助我。
这只是POST的问题,所有的GET请求都工作正常,所以签名和时间戳...或者至少我得到了我的帐户的响应,所以我假设是这样。
第一个CURL仅用于serverTime,而第二个CURL用于买入/卖出订单。
这是我现在得到的回应...
对象([代码]=〉-1102 [消息]=〉强制参数"side"未发送、为空或格式错误。)
如果我手动输入字符串,它工作正常,但是由于这样或那样的原因,当我传递$qs时,它出现了上面的错误。我将$qs回显到屏幕上,并复制了它,而不是传递$qs,当我输入新的时间戳时,它工作正常。我被难住了...
"符号= TRXUSDC &侧面=卖出&类型=限制&生效时间= GTC &数量= 63000.0000000 &价格= 0.02550000 &接收窗口= 1000000 &时间戳= 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);
okxuctiv

okxuctiv1#

我尝试了以下代码和var_dump $qs

symbol=BTCUSDT&side=BUY&...

因此我认为您必须删除amp;

xlpyo6sf

xlpyo6sf2#

基于Binance spot 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"
  ]
}

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

kqhtkvqz

kqhtkvqz3#

我测试了代码,发现了一些小故障:

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

给我一个错误({“code”:-1102,“msg”:“强制参数'side'未发送,为空/null,或格式错误。"}),所以我将其替换为:

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

之后,您创建了签名,但从未将其添加到请求中。因此,请执行以下操作:

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

同时更新你的“recvWindow”到最大60000(我建议50000或更少(见@Tofiq Samali的答案)),你就可以开始了!

相关问题