Search code examples
phpcoinbase-php

How to read a value from an object


So I got the next output from a print_r

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

I'm using Coinbase SDK -> link to github

My question is how am I supposed to read the amount value? I'm generating that using

$buyPrice = $client->getSpotPrice('BTC-USD');

and getSpotPrice function is ->

 public function getSpotPrice($currency = null, array $params = [])
{
    if (strpos($currency, '-') !== false) {
        $pair = $currency;
    } else if ($currency) {
        $pair = 'BTC-' . $currency;
    } else {
        $pair = 'BTC-USD';
    }

    return $this->getAndMapMoney('/v2/prices/' . $pair . '/spot', $params);
}

saw something like this in the test integrations but I can't tell how to make this work:

public function testGetSpotPrice1()
{
    $price = $this->client->getSpotPrice();

    $this->assertInstanceOf(Money::class, $price);
}

Any help/ideas will be appreciated , thank you!


Solution

  • Once you've got the value by

    $buyPrice = $client->getSpotPrice('BTC-USD');
    

    You can then use (from the source https://github.com/coinbase/coinbase-php/blob/master/src/Value/Money.php )...

    $amount = $buyPrice->getAmount();
    $currency = $buyPrice->getCurrency();