【发布时间】:2014-03-12 08:42:24
【问题描述】:
在这方面需要帮助....我需要从 URL 的 API 调用中获取 json 数据。它说被调用的需要..
- 内容类型:application/x-www-form-urlencoded
- HTTP 标头:密钥 ---> APIKEY
- HTTP HEADERS : sig ---> 带有 SECRET KEY 的 POST 数据的 HMAC-SHA1 签名
- 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); -
我提出了第一个问题,就像没有人回答它一样。所以我尝试了第二个。对不起。只需要紧急修复此代码。