Binance REST API - 通过查询字符串下达 PHP 订单 (POST)
Posted
技术标签:
【中文标题】Binance REST API - 通过查询字符串下达 PHP 订单 (POST)【英文标题】:Binance REST API - Placing a PHP Order (POST) via Query String 【发布时间】:2019-01-05 11:09:20 【问题描述】:我正在努力使用 Binance 的 REST API。我已经设法通过查询字符串(例如 ping 服务器、股票信息等)获得有效的 GET 请求。我现在的挑战是使用 cURL 通过查询字符串执行 POST 请求。我一直在从各个地方抓取代码并参考 API 以使各个部分正常工作,但我不确定为什么会从结果中返回此错误... "code":-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×tamp=".$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×tamp=".$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;
【问题讨论】:
请确保您在抓取之前遵守网站的条款和条件 【参考方案1】:根据他们的 API 文档:
SIGNED 端点需要在 查询字符串 或 请求正文 中发送附加参数签名。
您不是通过这两种方法发送签名,而是通过标头发送签名。
改变这个:
curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=TRXBTC&type=market&side=buy&quantity=100.00&recvWindow=10000000000000000×tamp=".$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×tamp=" . $timestamp . "&signature=" . $signature);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apikey));
【讨论】:
"code":-1022,"msg":"Signature for this request is not valid." .... 这是我收到的新错误。我只是在做完全错误的签名吗?似乎我在签名中输入了两次查询字符串,然后在后字段中输入了两次。 :// 好的,这与您之前遇到的错误不同,因此您正在变暖。现在您只需要修复创建签名的方式就可以了。我相信我已经回答了您最初的问题,不是吗? 实际上,检查他们的文档,您会发现您没有像他们在文档示例中显示的那样创建签名:symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559
您似乎在这里缺少一些键...
这真的很奇怪。我接受了您的建议并使用了他们提供的默认查询字符串,除了我将 recvWindow 保持得更高(我不担心它此时不会执行只是功能),现在它可以工作了。我之前使用的查询字符串是下 MARKET 订单而不是 LIMIT 订单的不同方式。我希望我可以回去用我的原始字符串将其修复为市场订单。但是感谢您的帮助,真的很感激:)
没问题,祝你好运。【参考方案2】:
<?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>';
?>
【讨论】:
【参考方案3】:这里是一个例子,使用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);
【讨论】:
以上是关于Binance REST API - 通过查询字符串下达 PHP 订单 (POST)的主要内容,如果未能解决你的问题,请参考以下文章
JAVA实战-01-binance量化机器人API接入(更新中)