3

我正在努力使用 Binance 的 REST API。我已经设法通过查询字符串(例如 ping 服务器、股票信息等)获得有效的 GET 请求。我现在的挑战是使用 cURL 通过查询字符串执行 POST 请求。我一直在从各个地方抓取代码并参考 API 以使各个部分正常工作,但我不确定为什么会从结果中返回此错误... {"code":-1102,"msg": “强制参数‘签名’未发送,为空/null,或格式错误。”}网页上显示的错误)。我回显了签名及其一堆乱码,所以我相信在顶部执行的 hash_hmac 会起作用,但老实说,让 GET 请求起作用我很幸运。有人对为什么会被破坏有任何建议吗?谢谢!

$apikey = "MYKEY";
$apisecret = "MYSECRET";

$timestamp = time()*1000; //get current timestamp in milliseconds
$signature = hash_hmac('sha256', "TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=".$timestamp, $apisecret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.binance.com/api/v3/order/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=".$timestamp);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey,"signature: ".$signature));
$response = curl_exec($ch);
curl_close($ch);

echo $response;
4

3 回答 3

5

根据他们的 API 文档:

SIGNED 端点需要在查询字符串请求正文中发送附加参数签名。

您不是通过这两种方法发送签名,而是通过标头发送签名。

改变这个:

curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=".$timestamp);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey,"signature: ".$signature));

对此:

curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000&timestamp=" . $timestamp . "&signature=" . $signature);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey));
于 2018-07-28T16:05:48.360 回答
3
<?php 

$secret = "F................";
$key = "D.................";

$s_time = "timestamp=".time()*1000;

$sign=hash_hmac('SHA256', $s_time, $secret);
    
$url = "https://api.binance.com/api/v3/account?".$s_time.'&signature='.$sign;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-MBX-APIKEY:'.$key));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);

$result = curl_exec($ch);

$result = json_decode($result, true);

echo '<pre>';
var_dump($result);
echo '</pre>';

?>
于 2020-09-16T17:45:02.083 回答
0

这是一个示例,使用php-curl-class

    // Variables
    // url, key and secret is on separate file, called using require once
    $endPoint = "/api/v3/order/test";
    $coin = "BTC";
    $fiat = "EUR";
    $symbol = $coin . "" . $fiat;
    $side = "BUY";
    $type = "LIMIT";
    $timeInForce = "GTC";
    $quantity = 1;
    $price = 10000;
    $timestamp = time();

    // Constructing query arrays
    queryArray = array(
        "symbol" => $symbol,
        "side" => $side,
        "type" => $type,
        "timeInForce" => $timeInForce,
        "quantity" => $quantity,
        "price" => $price,
        "timestamp" => $timestamp*1000
    );
    $signature = hash_hmac("sha256", http_build_query($queryArray), $secret);
    $signatureArray = array("signature" => $signature);
    $curlArray = $queryArray + $signatureArray;

    // Curl : setting header and POST
    $curl->setHeader("Content-Type","application/x-www-form-urlencoded");
    $curl->setHeader("X-MBX-APIKEY",$key);

    $curl->post($url . "" . $endPoint, $curlArray);

    if ($curl->error) {
        echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
    }
    $order = $curl->response;
    print_r($order);
于 2020-06-03T14:28:31.703 回答