PHP curl hash_hmac

PHP curl hash_hmac,php,curl,hmacsha1,Php,Curl,Hmacsha1,在这方面需要帮助。。。。我需要从URL的API调用中获取json数据。它说,需要 内容类型:application/x-www-form-urlencoded HTTP头:键-->APIKEY HTTP头文件:sig-->HMAC-SHA1带有密钥的POST数据签名 POST参数:timestamp-->当前Unix时间戳 这是我的密码 $key = 'APIKEY'; $secret = 'APISECRET'; $timestamp = time(); $signature = hash

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

  • 内容类型:application/x-www-form-urlencoded
  • HTTP头:键-->APIKEY
  • HTTP头文件:sig-->HMAC-SHA1带有密钥的POST数据签名
  • POST参数:timestamp-->当前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!”}。 有什么线索吗

    $key = 'APIKEY';
    $secret_key = 'APISECRET';
    
    $timestamp = time(); 
    $signature = hash_hmac('sha1', $timestamp, $secret);
    
    应该是

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

    您的$secret未定义。除非您只是没有粘贴它,否则将导致错误

    {“错误”:“无效签名!”}您可以更改
    curl_setopt($ch,CURLOPT_POSTFIELDS,“timestamp=.time())
    to
    curl_setopt($ch,CURLOPT_POSTFIELDS,“timestamp=”.$timestamp)
    以发布签名中使用的相同值。事实上,签名是错误的,您定义了
    $secret\u key
    ,但使用
    $secret
    进行散列<代码>$signature=hash_hmac('sha1',$timestamp,$secret_key)