Signature rules

Signature rules

1. Splice x-api-key, x-api-nonce, and x-api-timestamp in the request hearer to 
obtain the string $str1
2. Concatenate $str1 and $str2 to obtain the string $str3
3. Use $str3 as the message and apisecret as the key, 
and perform sha256 hash algorithm operation to obtain the signature string sign

Precautions

1. If there is a url address in the request parameter, it needs to be urlencoded.

Signature example

<?php

$apikey    = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$apisecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$header = [
    'x-api-key'       => $apikey,
    'x-api-uid'       => '1001',
    'x-api-nonce'     => 'abc',
    'x-api-timestamp' => 1677227675,
    'x-api-signature' => '',
];
$params = [
    'out_trade_no'    => 'ac0948s23421123',
    'assets'          => 'TRC20-USDT',
    'amount'          => 100,
    'notify_url'      => urlencode('http://baidu.com'),
];

$args = [];
foreach ($params as $key => $val) {
    if ( !empty($val) ) $args[$key] = $val;
}
ksort($args);

// 1. Splice x-api-key, x-api-nonce, and x-api-timestamp in the request hearer to obtain the string str1
$str1 = $header['x-api-key'] . $header['x-api-nonce'] . $header['x-api-timestamp'];
// 2. After sorting the interface parameter set from small to large in ASCII code (lexicographic order), the string str2 is obtained by splicing it together according to the key=value method.
$str2 = http_build_query($args);
// 3. Concatenate str1 and str2 to obtain the string str3
$str3 = $str1 . $str2;
// 4. Use str3 as the message and apisecret as the key, and perform sha256 hash algorithm operation to obtain the signature string sign
$sign = hash_hmac('sha256', $str3, $apisecret);

$header['x-api-signature'] = $sign;

// echo "sign: {$sign}";

Last updated