1

I want to send btc using my account with api with php.

I tried coinbase api from github which is depreciated

https://github.com/coinbase/coinbase-php

and i am getting error when i tried this code

require_once( __DIR__ . '/requires/coinbase/vendor/autoload.php');


use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Enum\CurrencyCode;
use Coinbase\Wallet\Resource\Transaction;
use Coinbase\Wallet\Value\Money;
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

 $transaction = Transaction::send([
     'toBitcoinAddress' => "$bticoin_address",
     'amount'           => new Money($amount, CurrencyCode::USD),
     'description'      => 'Your first bitcoin!',
     'fee'              => '0.0001' // only required for transactions under BTC0.0001
     ]);
     try { 
        $transaction = $client->createAccountTransaction($account, $transaction);                   
        }
        catch(Exception $e) {
          echo $e->getMessage(); 
        }

and i am getting this error

<b>Fatal error</b>:  Uncaught TypeError: Argument 1 passed to Coinbase\Wallet\Exception\HttpException::exceptionClass() must be an instance of Psr\Http\Message\ResponseInterface, null given, called in /home/fiberpay/public_html/owner/requires/coinbase/vendor/coinbase/coinbase/src/Exception/HttpException.php on line 33 and defined in /home/fiberpay/public_html/owner/requires/coinbase/vendor/coinbase/coinbase/src/Exception/HttpException.php:98
Stack trace:
#0 /home/fiberpay/public_html/owner/requires/coinbase/vendor/coinbase/coinbase/src/Exception/HttpException.php(33): Coinbase\Wallet\Exception\HttpException::exceptionClass(NULL)
#1 /home/fiberpay/public_html/owner/requires/coinbase/vendor/coinbase/coinbase/src/HttpClient.php(137): Coinbase\Wallet\Exception\HttpException::wrap(Object(GuzzleHttp\Exception\RequestException))
#2 /home/fiberpay/public_html/owner/requires/coinbase/vendor/coinbase/coinbase/src/HttpClient.php(121): Coinbase\Wallet\HttpClient-&gt;send(Object(GuzzleHttp\Psr7\Request), Array)
#3 /home/fiberpay/public_html/owne in <b>/home/fiberpay/public_html/owner/requires/coinbase/vendor/coinbase/coinbase/src/Exception/HttpException.php</b> on line <b>98</b><br />

Is there anyway to do that ??

I spend much time to google it but still not found any solution that is i why i put that on there may be i got some good solution from there :)

Best Regars

Mohammad Umer
  • 542
  • 5
  • 27
  • 1
    Try to debug within the source code. For example. dump the RequestException in the HttpException class or follow the error path and dump the expected results. Maybe you can find something. In the most extreme case, you could alter the package itself, as long as it is deprecated already... – sanderbee Dec 28 '20 at 07:31
  • I saw the same problem with someone else https://stackoverflow.com/questions/61735624/coinbase-php-lib-not-working-fine-showing-errors and somone answer that we have to use python :D – Mohammad Umer Dec 28 '20 at 07:34
  • Same here but no one answered https://stackoverflow.com/questions/48858834/coinbase-api-catchable-fatal-error-null-argument-to-httpexceptionexceptionclas?rq=1 – Mohammad Umer Dec 28 '20 at 07:50
  • Considering that the library is deprecated and haven't gotten any updates for about 2 years, it could be many issues with it (even the API might have changed during that time.) You need to debug this yourself and try and fix any issues you get, or read the API documentation and build your own wrapper, if you can't find any that's still supported. – M. Eriksson Dec 28 '20 at 07:54

2 Answers2

0

Instead of using coinbase PHP wrapper use coinbase API itself

To send money from one coinbase account to another you can use the below code

first, get api_key and api_secret

$private_key = 'Doadlraixxxxxx'; //Coinbase Private Key
$api_key = 'ysEZjjBxxxxx'; //Coinbase API KEY

The Create a new stdClass object which contains your request method for withdrawing it will be POST, the path will be API path, and for withdrawing it will be /v2/accounts/:accountid/transactions, and then simply put private key and API key here

$json = new stdClass();
$json->method = "POST";
$json->path = "/v2/accounts/".$account_id."/transactions";
$json->secretapikey = $private_key;
$json->apikey = $api_key;

Now create another stdClass that contain your withdrawal info like email, currency, etc

$body = new stdClass();
$body->type = "send";
$body->to = $to_email;
$body->amount = $amount;
$body->currency = $currency;

then in the next step, we have to create a signature according to the timestamp for this we have to create SHA256 hash of current time, path, and the $body stdClass

$result = json_encode($json);
$body = json_encode($body);
$time= time();
$sign = $time.$json->method.$json->path.$body;
$hmac = hash_hmac("SHA256", $sign, $json->secretapikey);

Our Signature and body are ready now just need to post it

$URL = "https://api.coinbase.com/v2/accounts/".$account_id."/transactions";
$header = array(
  "CB-ACCESS-KEY:".$api_key,
  "CB-ACCESS-SIGN:".$hmac,
  "CB-ACCESS-TIMESTAMP:".time(),
  "CB-VERSION:2019-11-15",
  "Content-Type:application/json"
   );

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

After sending a request the money will be sended successfully.

And with a little decoration code will look like this https://gist.github.com/AdityaEXP/27408bd361d5b8fcc9ac2bf459eb5303

Aditya Dev
  • 100
  • 1
  • 8
  • Please add all explanation to your answer instead of linking to external ressources – Nico Haase Oct 19 '21 at 08:05
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. – AdityaDees Oct 19 '21 at 08:06
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – U13-Forward Oct 19 '21 at 08:10
  • ok fixed tell me if its understable otherwise I will edit – Aditya Dev Oct 19 '21 at 08:18
  • Please share more details. How does your code resolve the initial problem? As far as I see, it doesn't use any of the API code shared in the question – Nico Haase Oct 19 '21 at 08:25
  • I thought he just want to send money from coinbase to another coinbase – Aditya Dev Oct 19 '21 at 08:27
-1

I've been using the coinbase php library for 4years. When you get that error, it's highly probably due to incorrect account credentials.

Also, the default github library has an expired uploaded certificate in one of its folder. You have to replace it. Coinbase PHP works fine for me everytime.

Sir'Energieman
  • 237
  • 1
  • 6