Coinbase API PHP 获取当前资产 - 嵌套对象数组

Coinbase API PHP get current assets - nested object array

我刚刚开始使用 Coinbase API PHP(版本 2)。

我正在尝试获取当前资产。

我能得到的最接近的方法是使用 getAccounts 函数,它 returns(看似)资产的完整历史记录,因此我试图从中提取当前资产。

代码:

include 'cfg.php'; // Contains the CoinBase API key and secret

require_once ('vendor/autoload.php');

use Coinbase\Wallet\Client as Client;
use Coinbase\Wallet\Configuration as Configuration;

$configuration = Configuration::apiKey($cbase_API_Key, $cbase_API_Secret);
$client = Client::create($configuration);

$account = $client->getAccounts();

echo '<pre>';
var_dump ($account);
echo '</pre>';

这会产生如下长度输出(截断):

object(Coinbase\Wallet\Resource\ResourceCollection)#41 (3) {
  ["previousUri":"Coinbase\Wallet\Resource\ResourceCollection":private]=>
  NULL
  ["nextUri":"Coinbase\Wallet\Resource\ResourceCollection":private]=>
  NULL
  ["resources":"Coinbase\Wallet\Resource\ResourceCollection":private]=>
  array(50) {
    [0]=>
    object(Coinbase\Wallet\Resource\Account)#39 (12) {
      ["name":"Coinbase\Wallet\Resource\Account":private]=>
      string(10) "NMR Wallet"
      ["primary":"Coinbase\Wallet\Resource\Account":private]=>
      bool(false)
      ["type":"Coinbase\Wallet\Resource\Account":private]=>
      string(6) "wallet"
      ["currency":"Coinbase\Wallet\Resource\Account":private]=>
      string(3) "NMR"
      ["balance":"Coinbase\Wallet\Resource\Account":private]=>
      object(Coinbase\Wallet\Value\Money)#47 (2) {
        ["amount":"Coinbase\Wallet\Value\Money":private]=>
        string(10) "0.71993154"
        ["currency":"Coinbase\Wallet\Value\Money":private]=>
        string(3) "NMR"
      }
      ["nativeBalance":"Coinbase\Wallet\Resource\Account":private]=>
      object(Coinbase\Wallet\Value\Money)#51 (2) {
        ["amount":"Coinbase\Wallet\Value\Money":private]=>
        string(5) "25.97"
        ["currency":"Coinbase\Wallet\Value\Money":private]=>
        string(3) "GBP"
      }
      ["createdAt":"Coinbase\Wallet\Resource\Account":private]=>
      object(DateTime)#52 (3) {
        ["date"]=>
        string(26) "2021-03-30 23:33:01.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
      }
      ["updatedAt":"Coinbase\Wallet\Resource\Account":private]=>
      object(DateTime)#53 (3) {
        ["date"]=>
        string(26) "2021-03-30 23:33:08.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
      }
      ["id":"Coinbase\Wallet\Resource\Resource":private]=>
      string(36) "YYYYYYYYYY"
      ["resource":"Coinbase\Wallet\Resource\Resource":private]=>
      string(7) "account"
      ["resourcePath":"Coinbase\Wallet\Resource\Resource":private]=>
      string(49) "/v2/accounts/YYYYYYYYYY"
      ["rawData":"Coinbase\Wallet\Resource\Resource":private]=>
      array(13) {
        ["id"]=>
        string(36) "YYYYYYYYYY"
        ["name"]=>
        string(10) "NMR Wallet"
        ["primary"]=>
        bool(false)
        ["type"]=>
        string(6) "wallet"
        ["currency"]=>
        string(3) "NMR"
        ["balance"]=>
        array(2) {
          ["amount"]=>
          string(10) "0.71993154"
          ["currency"]=>
          string(3) "NMR"
        }
        ["created_at"]=>
        string(20) "2021-03-30T23:33:01Z"
        ["updated_at"]=>
        string(20) "2021-03-30T23:33:08Z"
        ["resource"]=>
        string(7) "account"
        ["resource_path"]=>
        string(49) "/v2/accounts/YYYYYYYYYY"
        ["allow_deposits"]=>
        bool(true)
        ["allow_withdrawals"]=>
        bool(true)
        ["native_balance"]=>
        array(2) {
          ["amount"]=>
          string(5) "25.97"
          ["currency"]=>
          string(3) "GBP"
        }
      }
    }
    [1]=>
    object(Coinbase\Wallet\Resource\Account)#54 (12) {
      ["name":"Coinbase\Wallet\Resource\Account":private]=>
      string(10) "REP Wallet"
      ["primary":"Coinbase\Wallet\Resource\Account":private]=>
      bool(false)
      ["type":"Coinbase\Wallet\Resource\Account":private]=>
      string(6) "wallet"
      ["currency":"Coinbase\Wallet\Resource\Account":private]=>
      string(3) "REP"
      ["balance":"Coinbase\Wallet\Resource\Account":private]=>
      object(Coinbase\Wallet\Value\Money)#55 (2) {
        ["amount":"Coinbase\Wallet\Value\Money":private]=>
        string(10) "0.00000000"
        ["currency":"Coinbase\Wallet\Value\Money":private]=>
        string(3) "REP"
      }
      ["nativeBalance":"Coinbase\Wallet\Resource\Account":private]=>
      object(Coinbase\Wallet\Value\Money)#56 (2) {
        ["amount":"Coinbase\Wallet\Value\Money":private]=>
        string(4) "0.00"
        ["currency":"Coinbase\Wallet\Value\Money":private]=>
        string(3) "GBP"
      }
      ["createdAt":"Coinbase\Wallet\Resource\Account":private]=>
      object(DateTime)#57 (3) {
        ["date"]=>
        string(26) "2021-03-29 21:31:04.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
      }
      ["updatedAt":"Coinbase\Wallet\Resource\Account":private]=>
      object(DateTime)#58 (3) {
        ["date"]=>
        string(26) "2021-03-30 23:33:07.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
      }
      ["id":"Coinbase\Wallet\Resource\Resource":private]=>
      string(36) "XXXXXXXXXX"
      ["resource":"Coinbase\Wallet\Resource\Resource":private]=>
      string(7) "account"
      ["resourcePath":"Coinbase\Wallet\Resource\Resource":private]=>
      string(49) "/v2/accounts/XXXXXXXXXX"
      ["rawData":"Coinbase\Wallet\Resource\Resource":private]=>
      array(13) {
        ["id"]=>
        string(36) "XXXXXXXXXX"
        ["name"]=>
        string(10) "REP Wallet"
        ["primary"]=>
        bool(false)
        ["type"]=>
        string(6) "wallet"
        ["currency"]=>
        string(3) "REP"
        ["balance"]=>
        array(2) {
          ["amount"]=>
          string(10) "0.00000000"
          ["currency"]=>
          string(3) "REP"
        }
        ["created_at"]=>
        string(20) "2021-03-29T21:31:04Z"
        ["updated_at"]=>
        string(20) "2021-03-30T23:33:07Z"
        ["resource"]=>
        string(7) "account"
        ["resource_path"]=>
        string(49) "/v2/accounts/XXXXXXXXXX"
        ["allow_deposits"]=>
        bool(true)
        ["allow_withdrawals"]=>
        bool(true)
        ["native_balance"]=>
        array(2) {
          ["amount"]=>
          string(4) "0.00"
          ["currency"]=>
          string(3) "GBP"
        }
      }
    }
.
.
  }
}

这看起来像嵌套对象数组。

我似乎无法访问和打印子项目。理想情况下,我想提取 nametypeamountcurrency 项。

最近尝试:

foreach ( $account as $acct )
{
   echo "=========<br>";
   var_dump ($acct);          // This works
   echo ".....<br>";
   $acc = get_object_vars($acct);

   foreach($acc as $key => $value) 
   { 
      echo "..... $key : $value <br>";  // Never gets here
   }
}

但这并没有遍历内部的 foreach。

我找到了答案 - 使用 public get 函数:

foreach ( $account as $acct )
{
   echo "=========<br>";
   var_dump ($acct);
   echo ".....<br>";
   echo 'Name:' . $acct->getName() . '<br>';
   echo 'Currency:' . $acct->getCurrency() . '<br>';
   echo 'Balance:' . $acct->getBalance()->getAmount() . '<br>';
}