PHP 卷曲 HMAC-SHA1 [英] PHP Curl HMAC-SHA1

查看:31
本文介绍了PHP 卷曲 HMAC-SHA1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Need help on this .... I need to get json data from API call from a URL. It said the called need..

  1. 内容类型:应用程序/x-www-form-urlencoded
  2. HTTP 标头:key ---> APIKEY
  3. HTTP HEADERS : sig ---> POST 数据的 HMAC-SHA1 签名,带有 SECRET KEY
  4. POST 参数:时间戳 ----> 当前 Unix 时间戳

我这样做正确吗?但是如何实现上面的第3点???

Do I do this correctly? But how to implement point 3 above???

$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 新手.

Been scratching my head over this. please help me this newbie on PHP.

推荐答案

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

Finally I got it.... see the $signature part.

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

这篇关于PHP 卷曲 HMAC-SHA1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆