Binance REST API - 通过查询字符串下达 PHP 订单 (POST)

Binance REST API - Placing a PHP Order (POST) via Query String

我正在努力使用 Binance 的 REST API。我已经设法通过查询字符串(例如对服务器执行 ping 操作、代码信息等)获得工作 GET 请求。我现在的挑战是使用 cURL 通过查询字符串执行 POST 请求。我一直在从不同的地方抓取代码并参考 API 来让部分工作,但我不确定为什么我从结果中返回这个错误...... {"code":-1102,"msg":"Mandatory parameter 'signature' was not sent, was empty/null, or malformed."} (ERROR SHOWN ON WEBPAGE)。我回显了签名及其一堆乱码,所以我相信在顶部执行的 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;

根据他们的 API 文档:

SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body.

您没有通过这两种方法发送签名,而是通过 header 发送签名。

改变这个:

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

这是一个例子,使用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);
<?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>';

?>