0

I am new bee to CoinBase.

I am using getAccounts() method to fetch all the accounts.

$this->client->getAccounts();

This is returning an object of type object(Coinbase\Wallet\Resource\ResourceCollection)

object(Coinbase\Wallet\Resource\ResourceCollection)[37]
  private 'previousUri' => null
  private 'nextUri' => null
  private 'resources' => 
    array (size=1)
      0 => 
        object(Coinbase\Wallet\Resource\Account)[35]
          private 'name' => string 'BTC Wallet' (length=10)
          private 'primary' => boolean true
          private 'type' => string 'wallet' (length=6)
          private 'currency' => string 'BTC' (length=3)
          private 'balance' => 
            object(Coinbase\Wallet\Value\Money)[43]
              ...
          private 'nativeBalance' => 
            object(Coinbase\Wallet\Value\Money)[47]
              ...
          private 'createdAt' => 
            object(DateTime)[48]
              ...
          private 'updatedAt' => 
            object(DateTime)[49]
              ...
          private 'id' (Coinbase\Wallet\Resource\Resource) => string 'b12d3892-1228-5da1-a779-c5104bcbd749' (length=36)
          private 'resource' (Coinbase\Wallet\Resource\Resource) => string 'account' (length=7)
          private 'resourcePath' (Coinbase\Wallet\Resource\Resource) => string '/v2/accounts/b12d3892-1228-5da1-a779-c5104bcbd749' (length=49)
          private 'rawData' (Coinbase\Wallet\Resource\Resource) => 
            array (size=11)
              ...

Converting the response using (array) gives invalid characters in the response.

   array (size=3)
  '�Coinbase\Wallet\Resource\ResourceCollection�previousUri' => null
  '�Coinbase\Wallet\Resource\ResourceCollection�nextUri' => null
  '�Coinbase\Wallet\Resource\ResourceCollection�resources' => 
    array (size=1)
      0 => 
        object(Coinbase\Wallet\Resource\Account)[35]
          private 'name' => string 'BTC Wallet' (length=10)
          private 'primary' => boolean true
          private 'type' => string 'wallet' (length=6)
          private 'currency' => string 'BTC' (length=3)
          private 'balance' => 
            object(Coinbase\Wallet\Value\Money)[43]
              ...
          private 'nativeBalance' => 
            object(Coinbase\Wallet\Value\Money)[47]
              ...
          private 'createdAt' => 
            object(DateTime)[48]
              ...
          private 'updatedAt' => 
            object(DateTime)[49]
              ...
          private 'id' (Coinbase\Wallet\Resource\Resource) => string 'b12d3892-1228-5da1-a779-c5104bcbd749' (length=36)
          private 'resource' (Coinbase\Wallet\Resource\Resource) => string 'account' (length=7)
          private 'resourcePath' (Coinbase\Wallet\Resource\Resource) => string '/v2/accounts/b12d3892-1228-5da1-a779-c5104bcbd749' (length=49)
          private 'rawData' (Coinbase\Wallet\Resource\Resource) => 
            array (size=11)
              ...

Any help will be appreciated.

Anoop

Anoop Pete
  • 492
  • 2
  • 4
  • 17
  • Why are you converting it to an array? – Mulan Jun 28 '16 at 20:32
  • @naomik : I want to pass the response to jquery code as json object and display. – Anoop Pete Jun 29 '16 at 03:31
  • I'm looking at [`Account.php`](https://github.com/coinbase/coinbase-php/blob/master/src/Resource/Account.php) and [`Resource.php`](https://github.com/coinbase/coinbase-php/blob/master/src/Resource/Resource.php). I can't find anyway to convert the PHP objects into JSON. At least in `Account.php` you can see it's pretty easy to get at the data you need. – Mulan Jun 29 '16 at 03:43
  • @naomik : can you share the code snippet or any reference links? – Anoop Pete Jun 29 '16 at 03:49
  • The first two files in my comment above are clickable links – Mulan Jun 29 '16 at 04:06

2 Answers2

0

Was able to rectify by the following code.

 $accountResponse = $this->client->getAccounts();
  foreach ($accountResponse->all() as &$account) {
  $responseData[] = $account->getRawData();
 }
Anoop Pete
  • 492
  • 2
  • 4
  • 17
-1

Try to convert like this:

$accounts = json_decode(json_encode($this->client->getAccounts()), true);

UPDATE

$accounts = $this->client->getAccounts();
$resources = $accounts->all();

foreach($resources as $index=>$details) {

  $accArr = [];

  foreach($details as $key=>$detail) {

    $method = 'get'.ucfirst($key).'()';
    $accArr[$key] = $detail->{$method};
    $accountsContainer[] = $accArr;

  }

}
var_dump($accountsContainer);
Yolo
  • 1,569
  • 1
  • 11
  • 16