Inserting data from Coinbase API V2 into a MySQL Database using php

408 views Asked by At

I'm a bit stuck here. I'm building an app based on the Coinbase PHP API V2.

This is what I'm trying to achieve. I would like to place the currency code (for e.g. EUR or USD) and the corresponding sell price at the moment of calling in my local MySQL database (ideal also including a timestamp).

I'm getting this info by using the method: getSellPrice(). This method returns this info:

Coinbase\Wallet\Value\Money Object ( [amount:Coinbase\Wallet\Value\Money:private] => 2250.47 [currency:Coinbase\Wallet\Value\Money:private] => USD )

So far so good. But how do I get these values saved in my database? Breaking it down I have the following sub-questions:

  1. How do extract the values "amount=>2250.47" and "currency=>USD"?
  2. How do I save these 2 values in the corresponding columns "amount" and "currency" in my database?
  3. What exact script do I need /steps to follow?

Just some general info. I'm using XAMPP as local server.

This is the code which I have so far:

`

<?php

require_once __DIR__ . '/vendor/autoload.php';
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;

//Connect with credentials.
$apiKey = 'xxx';
$apiSecret = 'xxx';

$configuration = Configuration::apiKey($apiKey, $apiSecret);

$client = Client::create($configuration);

$sellPrice = $client->getSellPrice('BTC-USD');
print_r ($sellPrice);

//So far it works fine.

?>

`

I have set up a table by using this query:

`CREATE TABLE `data` (
`id` BIGINT UNSIGNED NOT NULL ,
`time_stamp` BIGINT UNSIGNED NOT NULL ,
`currency` VARCHAR( 255 ) NOT NULL ,
`amount` BIGINT UNSIGNED NOT NULL ,
PRIMARY KEY ( `id` ) 
);

`

Every help is welcome or pointing me in the right direction. And is much appreciated!

Cheers

2

There are 2 answers

1
mighTY On

There is a method, which gets you the response in a raw format.

Check out $data = $client->decodeLastResponse();

It's taken from the original documentation on git.

0
Harsh Panchal On

TRY This code to get amount and currency

$sellPrice = $client->getSellPrice('BTC-USD');
$amount=$sellPrice->getAmount();                                
$currency=$sellPrice->getCurrency();

Hope it will work