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

时间:2018-07-28 15:56:10

标签: php rest api curl binance

我正在努力使用Binance的REST API。我设法通过查询字符串(例如ping服务器,股票行情信息等)来获取有效的GET请求。我现在的挑战是使用cURL通过查询字符串执行POST请求。我一直在各地收集代码,并返回API进行工作,但是我不确定为什么从结果中返回此错误... {“代码”:-1102,“ msg“:“必填参数'signature'未发送,为空/空或格式错误。”} ERROR SHOWN ON WEBPAGE)。我回显了签名及其繁琐的工作,因此我相信在顶部执行的hash_hmac会起作用,但是老实说,我很幸运使GET请求起作用。有谁对为什么会破坏有任何建议吗?谢谢!

other.y

3 个答案:

答案 0 :(得分:1)

根据他们的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));

答案 1 :(得分: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);

答案 2 :(得分:0)

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

?>