PHP Curl HMAC-SHA1

好久不见. 提交于 2019-11-30 21:12:43

问题


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

  1. Content-Type : application/x-www-form-urlencoded
  2. HTTP HEADERS : key ---> APIKEY
  3. HTTP HEADERS : sig ---> HMAC-SHA1 signature of POST Data with SECRET KEY
  4. POST PARAMETER: timestamp ----> Current Unix Timestamp

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;

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


回答1:


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;


来源:https://stackoverflow.com/questions/22326050/php-curl-hmac-sha1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!