【问题标题】:Binance Pay: "Signature for this request is not valid" error with PHP cURLBinance Pay:PHP cURL 出现“此请求的签名无效”错误
【发布时间】:2021-09-03 14:32:57
【问题描述】:

我正在使用 PHP cURL 来连接 Binance Pay API。我收到以下错误:

{"status":"FAIL","code":"400002","errorMessage":"Signature for this request is not valid."}

这是我的代码:

// Generate nonce string
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$nonce = '';
for($i=1; $i <= 32; $i++)
{
    $pos = mt_rand(0, strlen($chars) - 1);
    $char = $chars[$pos];
    $nonce .= $char;
}
$ch = curl_init();
$timestamp = round(microtime(true) * 1000);
// Request body
$request = array(
    "merchantTradeNo" => "12485634875fJhdd56",
    "totalFee" => 15,
    "productDetail" => "productDetail",
    "currency" => "BUSD",
    "returnUrl" => "",
    "tradeType" => "WEB",
    "productType" => "productType",
    "productName" => "ProductName"
);
$json_request = json_encode($request);
$payload = $timestamp."\n".$nonce."\n".$json_request."\n";
$signature = strtoupper(hash_hmac('SHA512',$payload,$binance_pay_secret));
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "BinancePay-Timestamp: $timestamp";
$headers[] = "BinancePay-Nonce: $nonce";
$headers[] = "BinancePay-Certificate-SN: $binance_pay";
$headers[] = "BinancePay-Signature: $signature";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://bpay.binanceapi.com/binancepay/openapi/order");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch);
if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); }
curl_close ($ch);

关于文档,一切似乎都很好,但事实并非如此,所以有问题。任何的想法?谢谢!

【问题讨论】:

  • 你能解决这个问题吗?我也遇到了同样的错误。

标签: php curl binance


【解决方案1】:

在 python 中(很可能在 PHP 中也是如此),json 编码通过添加空格和/或转义引号来稍微修改请求 json。 Binance Pay API 要求 json 字符串与文档中的完全一致。也就是说,没有多余的空格。

【讨论】:

    【解决方案2】:

    你忘了添加这个

    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_request);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 2021-08-02
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      相关资源
      最近更新 更多