CoinBase“无效签名”PHP购买API请求

时间:2015-02-20 05:13:51

标签: php cryptography coinbase-api coinbase-php

我已多次查看代码,但每当我向API发送请求时,它都会返回"消息":"无效签名"

我认为这可能与哈希身体有关。我是PHP的新手,也是我的第一个项目。 :)

有人看到错误吗?感谢。

<?php
$arr = array('size' => ".01", 'price' => '240', 'side' => 'sell',
 'product_id' => 'BTC-USD');

$output = json_encode($arr);

echo json_encode($arr)."<br/>";



$key = "f23612b06cb4d020cda7e04b1ae6ef9a";
$secret = "RENqodtuTCn4v7g7Pn/FFdQAIKReVXGayNPrNN/Zb7AjATI0hP4R0MCDD5RqnDu60qTZ5Qry329fFu7kcObGBw==";
$passphrase = "tradebot";

$time = time();
$url = "https://api.gdax.com/orders";

$data = $time."POST"."/orders";
echo $data . "<br/>";

$hashinput = "$output"."$data";

$sign = base64_encode(hash_hmac("sha256", $hashinput, base64_decode($secret), true));                
echo $sign . "<br/>";

$headers = array(                
    'CB-ACCESS-KEY: '.$key,
    'CB-ACCESS-SIGN: '.$sign,
    'CB-ACCESS-TIMESTAMP: '.$time,
    'CB-ACCESS-PASSPHRASE: '.$passphrase,
    'Content-Type: application/json'
);

var_dump($headers);

echo $url;

static $ch = null;

if (is_null($ch)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'local server');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, $output);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $res = curl_exec($ch);
    echo $res;
}

1 个答案:

答案 0 :(得分:3)

替换

$hashinput = "$output"."$data";

$hashinput = "$data"."$output";

并替换

curl_setopt($ch, CURLOPT_POST, $output);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $output);

(来自Coinbase Community论坛)