CoinBase “invalid signature” PHP Buy API Request

2019-06-13 16:16发布

问题:

I have looked over the code many times but whenever I send request to API it returns "message":"invalid signature"

I am thinking it has to do with hashing the body, possibly. I'm new to PHP and its my first project. :)

Anyone see an error? Thanks.

<?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:

replace

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

with

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

and replace

curl_setopt($ch, CURLOPT_POST, $output);

with

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $output);

(from Coinbase Community forum)