I've written this simple PHP code with an explanation for each comma
PHP code
<?php
$lastBlockEnd = $this->MyRPC->BitcoinCoreCommand('getblockcount' , []);
// https://bitcoin.org/en/developer-reference#getblockcount
for ($i = 0; $i <= $lastBlockEnd; $i++){
$blockHash = $this->MyRPC->BitcoinCoreCommand('getblockhash' , [$i]);
//https://bitcoin.org/en/developer-reference#getblockhash
$blockData = $this->MyRPC->BitcoinCoreCommand('getdata' , [$blockHash]);
//https://bitcoin.org/en/developer-reference#getblock
$block[$i]['time'] = $blockData['time'];
$block[$i]['hash'] = $blockHash;
}
print_r($block);
Code Explanation
- We are getting total available blocks number, say its 65443
- we will start looping from block number 0 to 65443
Note: You should send "batch request" instead of calling each block separately
- we are getting block hash, then we're calling getdata and passing that block hash.
Now we've got the following response:
{
"hash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
"confirmations": 447014,
"strippedsize": 215,
"size": 215,
"weight": 860,
"height": 1,
"version": 1,
"versionHex": "00000001",
"merkleroot": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
"tx": [
"0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
],
"time": 1231469665,
"mediantime": 1231469665,
"nonce": 2573394689,
"bits": "1d00ffff",
"difficulty": 1,
"chainwork": "0000000000000000000000000000000000000000000000000000000200020002",
"previousblockhash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
"nextblockhash": "000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd"
}
Finally, We are storing time and block hash in an array and printing it.
Try to use 'bitcoin database viewer' https://sourceforge.net/projects/bitcoin-database-viewer/ This console app can convert bitcoin database from raw format to human readable view without using of any sites or APIs. So when you parse dat files with that app, you will see all that you need.
– D L – 2018-04-18T00:32:08.583