Manipulate and query RPC responses
2.2.xResponses from the client are returned as objects that support various data access patterns and helper methods for navigating JSON structures.
Data Access
- Method call as function:
$block('hash')->get()retrieves the value of the 'hash' key. - Array access:
$block['height']retrieves the value of the 'height' key. - Dot notation:
$block->get('tx.0')retrieves the first element of the 'tx' array.
Navigation and Filtering
$block->count('key'): Returns the count of elements in a key.$block->has('key'): Checks if a key exists and is NOT null.$block->exists('key'): Checks if a key exists (can be null).$block->contains($value): Checks if the response contains a specific value.$block->values(): Returns an array of all values.$block->keys(): Returns an array of all keys.$block->first('key')/$block->last('key'): Gets the first or last element of a specific key.$block->random(count, 'key'): Returns a random element or multiple random elements from a key.
$block = $bitcoind->getBlock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f);
$block('hash')->get(); // 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
$block['height']; // 0 (array access)
$block->get('tx.0'); // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
$block->count('tx'); // 1
$block->has('version'); // key must exist and CAN NOT be null
$block->exists('version'); // key must exist and CAN be null
$block->contains(0); // check if response contains value
$block->values(); // array of values
$block->keys(); // array of keys
$block->random(1, 'tx'); // random block txid
$block('tx')->random(2); // two random block txid's
$block('tx')->first(); // txid of first transaction
$block('tx')->last(); // txid of last transaction