Search code examples
phpcoinbase-api

send btc api coinbase?


I am trying to send satoshis to emails via php with the coinbase api, but it does not work for me. I saw this code online. I get this error:

{"errors": [{"id": "authentication_error", "message": "invalid signature"}]}

I attached the php code that I have on my website

<?php
$timestamp = time();
$method = 'POST';
$request_path = '/v2/accounts/34en86m3-b0qa-5022-a45c-b110z38631k6/transactions';
$body = 'type=send&[email protected]&amount=0.00002504&currency=BTC';

$account_id = '34en86m3-b0qa-5022-a45c-b110z38631k6';
$hash_input = $timestamp.''.$method.''.$request_path.''.$body;
$apiSecret = 'VmQruPgmAYsW6Pq1vsC5bnzObd5LpTIn';
$signature = hash_hmac('sha256', $hash_input, $apiSecret, true); 

$accesskey = '1XJZLVA1F4zjQ9cO';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.coinbase.com/v2/accounts/'.$account_id.'/transactions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');


$headers = array();
$headers[] = 'Cb-Access-Key: '.$accesskey;
$headers[] = 'Cb-Access-Sign: '.$signature;
$headers[] = 'Cb-Access-Timestamp: '.$timestamp;
$headers[] = 'Cb-version: 2017-08-07';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
else
{
echo $result;
}
curl_close ($ch);

?>  

Note: the key and secret codes posted in the code are invented, but on my website I have the originals.


Solution

  • I've solved it this way.

              $secret = "fffffffff"; //you number secret
              $timestamp = time();
              $method = "POST";
              $request_path = "/v2/accounts/fffff-fffff-ffff-ffff-ffff/transactions"; //you account id
                   
                   $data = array (
                   'type' => 'send',
                   'to' => '[email protected]',  //put email
                   'amount' => '0.0000012'), //put amount
                   'currency' => 'BTC',  //put crypto
                   'description' => 'put you description', //put description
                    );
                   $body = json_encode($data);
                   $prehash = $timestamp.$method.$request_path.$body;
                   $sign = hash_hmac("sha256", $prehash, $secret);
                   $ch = curl_init();
                   $headers = array(
                   'CB-ACCESS-KEY: fffffffffff', //You Key
                   'CB-ACCESS-SIGN: '.$sign,
                   'CB-ACCESS-TIMESTAMP: '.$timestamp,
                   'CB-ACCESS-VERSION: 2018-05-20',
                   'Content-Type: appliaction/json'
                   );
                   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                   curl_setopt($ch, CURLOPT_USERAGENT, $SERVER['HTTP_USER_AGENT']);
                   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                   curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
                   curl_setopt($ch, CURLOPT_URL, 'https://api.coinbase.com'.$request_path);
                   $res = curl_exec($ch);