gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:16:46 26 4
gpt4 key购买 nike

我在使用币安的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com