Response Methods
Following methods are provided by response object.
They are similar to laravel collections and can be used
for simple data filtering and manipulation.
You can even get response data as laravel collection using
collect()
method.
get()
Gets value by key provided as dot notation.
// $block = bitcoind()->getBlock($blockhash);
echo $block->get('tx.0');
When used with empty key, returns whole array.
// $block = bitcoind()->getBlock($blockhash);
print_r($block->get());
first()
Gets first element in array. Especially useful when paired with path lookup.
// $block = bitcoind()->getBlock($blockhash);
echo $block()->first('tx'); // first txid in the block
echo $block('tx')->first(); // same as above
last()
Gets last element in array. Especially useful when paired with path lookup.
// $block = bitcoind()->getBlock($blockhash);
echo $block()->last('tx'); // last txid in the block
echo $block('tx')->last(); // same as above
count()
Counts how many elements are in array.
// $block = bitcoind()->getBlock($blockhash);
echo $block->count('tx');
echo $block('tx')->count(); // same as above
has()
Checks if response has key certain and it’s value is NOT null.
// $block = bitcoind()->getBlock($blockhash);
if ($block->has('version')) {
// 'version' field exists within block data
}
exists()
Checks if response has certain key. It’s value CAN BE null.
// $block = bitcoind()->getBlock($blockhash);
if ($block->exists('version')) {
// 'version' field exists within block data
}
contains()
Checks if response contains value.
// $block = bitcoind()->getBlock($blockhash);
if ($block->contains('547285409')) {
// block data contains 547285409 (it's nonce)
}
values()
Gets array of values.
// $block = bitcoind()->getBlock($blockhash);
print_r($block->values());
keys()
Gets array of keys.
// $block = bitcoind()->getBlock($blockhash);
print_r($block->keys());
random()
Gets random element(s). Especially useful when paired with path lookup.
// $block = bitcoind()->getBlock($blockhash);
echo $block('tx')->random(/* number of elements to get */ 2);
echo $block()->random(2, 'tx'); // same as above
flatten()
Flattens multi-dimensional array into single-dimensional one. For this example we’ll use listUnspent() method that returns a list of UTXO belonging to this wallet
// $response = bitcoind()->listUnspent();
// array of addresses with non-zero balance
print_r($response->flatten('*.address'));
sum()
Gets sum of values. For this example we’ll use listSinceBlock() method that returns all transaction affecting wallet from certain block (in this example - genesis)
// $response = bitcoind()->listSinceBlock();
// sum of transactions affecting this wallet
echo $response->sum('transactions.*.amount');
collect()
Gets response data as laravel collection.
Check Laravel Documentation for a complete list of available methods.
// $block = bitcoind()->getBlock($blockhash);
$block->collect('tx')->each(function ($txid) {
// do something with tx id's
});
You can learn more about these methods by looking at the source code.