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

sauutmhj  于 2022-12-17  发布在  PHP
关注(0)|答案(4)|浏览(133)

我在使用Binance的API时遇到了困难,我已经设法通过查询字符串(如ping服务器、股票代码信息、我现在面临的挑战是使用cURL通过查询字符串执行POST请求。我一直在从各个地方抓取代码,并返回到API以使其工作,但我不确定为什么从结果中返回此错误...{”代码”:-1102,“msg”:“强制参数”signature“未发送,为空/null,或格式错误。"}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;
u91tlkcl

u91tlkcl1#

根据其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));
uelo1irk

uelo1irk2#

<?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>';

?>
h7appiyu

h7appiyu3#

以下是使用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);
8wtpewkr

8wtpewkr4#

我也遇到了同样的问题,上面的这些都没有帮助。所以我终于找到了如何在路上订购。所以,也许这能帮助到一些人。

function Kupovina($buy_parametri) {
$key = "xxxxxxxxxxxxxxx";
$secret = "xxxxxxxxxxxx";
$s_time = "timestamp=".time()*1000;
$timestamp = time()*1000; //get current timestamp in milliseconds
$sign = hash_hmac('sha256', $buy_parametri."&timestamp=".$timestamp, $secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.binance.com/api/v3/order");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $buy_parametri."&".$s_time."&signature=".$sign);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$key));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}

$buy_parametri = "symbol=BTCUSDT&type=market&side=buy&quantity=0.00086";

调用函数:

Kupovina($buy_parametri);

相关问题