CoinBase “invalid signature” PHP Buy API Request

倖福魔咒の 提交于 2019-12-08 09:31:49

问题


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)



来源:https://stackoverflow.com/questions/28622045/coinbase-invalid-signature-php-buy-api-request

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