2

I'm trying to use the API for Coinbase but I get invalid signature.

So probably I'm actually sign it wrong or I'm missing something.

what should I use on request? should i use POST or GET on method?

$urlapi = "https://api.coinbase.com/v2/time";
            $Key = "--------------";
            $Secret = "------------";               
            $fecha = new DateTime();
            $timestamp = $fecha->getTimestamp();
            $request="";
            $body="";
            $method="GET";  
            $Datas = $timestamp . $method . $request . $body;               
            $hmacSig = hash_hmac('sha256',$Datas,$Secret);
            $curl = curl_init($urlapi);
            curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json','CB-ACCESS-KEY: '.$Key,'CB-VERSION: 2015-07-07','CB-ACCESS-TIMESTAMP: '. $timestamp,'CB-ACCESS-SIGN: '.$hmacSig));
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            $resp = curl_exec($curl);
            if(!curl_exec($curl)){
            die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
            }
            curl_close($curl);

            print_r($resp);
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Donnie Darko
  • 509
  • 3
  • 15

3 Answers3

3

Get Current Time is a GET request (ref). The HTTP verb (GET/POST etc.) can be found in the docs for each type of request here:

enter image description here

In your example, the problem is that the $request variable in your message is blank. It should be $request="/v2/time";

hash_hmac returns hex encoded string by default so the hashing part is correct.

Weel
  • 132
  • 2
  • 7
mardlin
  • 292
  • 2
  • 12
  • That worked perfect, im trying now /v2/accounts so i can get a list of my accounts, which is only one. is still GET request? because im getting invalid signature using the code above. – Donnie Darko Jul 31 '15 at 00:19
  • And where im suppose to bin2hex? bin2hex($hmacSig) or bin2hex($Datas) – Donnie Darko Jul 31 '15 at 00:36
1
define('API_KEY', 'xxxx');
define('API_SECRET', 'xxxxxxxxx');
define('API_BASE', 'https://api.coinbase.com');
define('API_VERSION', '2015-08-31');

$headers = array(
      'Content-Type: application/json',
      'CB-VERSION: ' . API_VERSION
    );

$url = API_BASE.'/v2/time';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$output = json_decode($response, true);
print_r($output);

will return

Array ( [data] => Array ( [iso] => 2015-12-01T10:35:58Z [epoch] => 1448966158 ) )
Soorajlal K G
  • 778
  • 9
  • 20
1

You must use GET method, because is just to get some information, authentication is no needed.

This is an easier way:

$time = file_get_contents('https://api.coinbase.com/v2/time');
$time = json_decode($time);
$time = $time->data->epoch;
joao
  • 382
  • 4
  • 12