php GDAX api客户端:签名无效{"消息":"签名无效"}

时间:2018-02-27 16:38:58

标签: php json rest curl gdax-api

我一直在努力让私有GDAX API发挥作用。当我执行以下脚本时,我期待一些JSON,如:

{
    "trade_id": 74,
    "product_id": "BTC-USD",
    "price": "10.00",
    "size": "0.01",
    "order_id": "d50ec984-77a8-460a-b958-66f114b0de9b",
    "created_at": "2014-11-07T22:19:28.578544Z",
    "liquidity": "T",
    "fee": "0.00025",
    "settled": true,
    "side": "buy"
}

我的(沙盒)帐户中的订单填充列表。但我得到的只是:

{
     "message":"invalid signature"
 }

这意味着我的签名有点格式错误。我在关于创建签名的GDAX手册中偷了这个功能,所以我可以使用这里的PHP脚本。     &#39 ;;

$adjust = 0;
$time = time() + $adjust;

error_reporting(E_ALL);
ini_set('display_errors', 1);

$api_base = 'https://api-public.sandbox.gdax.com';
$api_url = '/fills';

// sign
function signature($request_path='', $body='', $timestamp=false, $method='GET', $secret) {
    $body = is_array($body) ? json_encode($body) : $body;
    $what = $timestamp.$method.$request_path.$body;
    return base64_encode(hash_hmac("sha256", $what, base64_decode($secret), true));
}


$api = 'https://api-public.sandbox.gdax.com';  // sandbox
$apikey = '9d9313b25c8d7872178c97cd09d0fa20';
$passphrase = '1k2lpf73rfg';
$secret = 'LJoPmgphrKbw8YWb+e7Wg2tn5RBCHRodkm+NI4Lhae0cu8ump3kVyHmLZdKWCvHPmOi0gvi/bO8nXucHj5TJmg==';

$request_path = $api_base . $api_url;
$body = '';

$signature = signature($request_path, $body, $time, 'GET', $secret);
//echo 'signature: ' . $signature;

/* curl */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_path); // set the api address
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // store the response in a variable
curl_setopt($ch, CURLOPT_HEADER, 1); // return the header
//curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); // transfer info
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36',
    'CB-ACCESS-KEY: ' . $apikey,
    'CB-ACCESS-SIGN: ' . $signature,
    'CB-ACCESS-TIMESTAMP: ' . $time,
    'CB-ACCESS-PASSPHRASE: ' . $passphrase
));
//$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//print_r($httpcode);

$output = curl_exec($ch);
if($output === FALSE){
    echo 'cURL ERROR: ' . curl_error($ch);
}else{
    echo 'successful connection:';
}

curl_close($ch);

echo $output;
print_r(json_decode($output));

echo '</pre>';
?>

0 个答案:

没有答案