PHP Curl HMAC-SHA1

Rai*_*Lai 1 php api curl hmacsha1

需要帮助....我需要从URL调用来自API调用的json数据.它说需要......

  1. 内容类型:application/x-www-form-urlencoded
  2. HTTP HEADERS:key ---> APIKEY
  3. HTTP HEADERS:sig --->带有SECRET KEY的POST数据的HMAC-SHA1签名
  4. POST PARAMETER:timestamp ---->当前的Unix时间戳

我这样做是否正确?但是如何实现上面的第3点???

$key = 'APIKEY';
$secret = 'APISECRET';
$signature = '';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.domain.com/getticker");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "timestamp=".time());
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","key: ".$key,"sig: ".$signature));
$response = curl_exec($ch);
curl_close($ch);

echo $response;
Run Code Online (Sandbox Code Playgroud)

一直在挠我的头.请帮我这个关于PHP的新手.

Rai*_*Lai 5

最后我明白了....看到$签名部分.

$key = 'APIKEY';
$secret = 'APISECRET';

$timestamp = time();
$signature = hash_hmac('sha1', "timestamp=".$timestamp, $secret);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.domain.com/getticker");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "timestamp=".time());
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","key: ".$key,"sig: ".$signature));
$response = curl_exec($ch);
curl_close($ch);

echo $response;
Run Code Online (Sandbox Code Playgroud)