PHP Curl HMAC-SHA1

时间:2014-03-11 12:40:52

标签: 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时间戳
  5. 我这样做是否正确?但是如何实现上面的第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的新手。

1 个答案:

答案 0 :(得分: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;