1

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

  1. 内容类型:应用程序/x-www-form-urlencoded
  2. HTTP 标头:密钥 ---> APIKEY
  3. HTTP HEADERS : sig ---> 带有 SECRET KEY 的 POST 数据的 HMAC-SHA1 签名
  4. POST PARAMETER: timestamp ----> Current Unix Timestamp

我这样做正确吗?但是如何实现上面的第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;

一直在为此挠头。请帮助我这个 PHP 新手。

4

1 回答 1

5

终于我明白了....见 $signature 部分。

$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;
于 2014-03-12T13:05:24.830 回答