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

标签 php rest api curl binance

我在使用币安的 REST API 时遇到困难。我已经成功地通过查询字符串获取工作 GET 请求,例如 ping 服务器、股票信息等。我现在的挑战是使用 cURL 通过查询字符串执行 POST 请求。我一直在从不同的地方抓取代码,并引用 API 来让代码正常工作,但我不确定为什么我会从结果中返回这个错误... {"code":-1102,"msg":"强制参数“签名”未发送、为空/空或格式错误。"} (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 - Binance REST API - 通过查询字符串下 PHP 订单 (POST),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51572971/

相关文章:

php - Laravel 在命名空间中找不到类

java - 配置 RestAssured 以使用 GSON 而不是 Jackson?

api - UPS 运输 REST API

rest - 通过(Rest)API 管理 AzureSearch

php - 选择行作为 wordpress post meta 的列

javascript - 在复选框勾选上隐藏表行内容

php - 生成 [M....N] 范围内的随机数,均值为 X

android - 将 REST 服务与 Android 应用程序同步

python - 需要建议在 python 中的 AWS API 中添加指数回退逻辑

java - 如何向所有人开放端点?