【问题标题】:PHP curl hash_hmac [duplicate]PHP curl hash_hmac [重复]
【发布时间】:2014-03-12 08:42:24
【问题描述】:

在这方面需要帮助....我需要从 URL 的 API 调用中获取 json 数据。它说被调用的需要..

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

这是我的代码...

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

$timestamp = time(); 
$signature = hash_hmac('sha1', $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;

它给了我错误消息 {"error":"Invalid Signature!"}。 有什么线索吗?

【问题讨论】:

  • {"error":"无效签名!"}
  • 您应将 curl_setopt($ch, CURLOPT_POSTFIELDS, "timestamp=".time()); 更改为 curl_setopt($ch, CURLOPT_POSTFIELDS, "timestamp=".$timestamp); 以发布您在签名中使用的相同值。
  • 其实你的签名是错误的,你定义了$secret_key,但是使用$secret来散列。 $signature = hash_hmac('sha1', $timestamp, $secret_key);
  • 我提出了第一个问题,就像没有人回答它一样。所以我尝试了第二个。对不起。只需要紧急修复此代码。

标签: php curl hmacsha1


【解决方案1】:
$key = 'APIKEY';
$secret_key = 'APISECRET';

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

应该是

$signature = hash_hmac('sha1', $timestamp, $secret_key);

您的 $secret 未定义。除非你只是没有粘贴它会导致错误

【讨论】:

  • 哎呀,我的错。实际上在我的代码中它是 $secret。不是 $secret_key。一定是在什么地方搞混了。
  • 哦,好吧,您可能想粘贴更多代码
  • 我再次编辑了这个问题,以反映我在我的代码中所做的事情,即 TOP 代码。
  • 你到底在使用哪个 api?
  • 来自 fybsg.com。文档位于docs.fyb.apiary.io 上,公共 API 不是问题。二等兵让我头疼。我想使用 API 函数 getaccinfo。
猜你喜欢
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
相关资源
最近更新 更多