PHP curl hash_hmac

时间:2014-03-12 08:42:25

标签: php 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. 这是我的代码......

    $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;
    

    它给了我错误消息{"错误":"无效签名!"}。 有线索吗?

1 个答案:

答案 0 :(得分:0)

$key = 'APIKEY';
$secret_key = 'APISECRET';

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

应该是

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

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