I'm trying to extract the bitcoin address from the blockchain. To be more specific if you go to this link https://blockchain.info/btc/block/1 you would see that the bitcoin address on that block is "12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
What is the best way to extract that details using PHP/Json ?
CodePudding user response:
take a look at blockchain.com/api/blockchain_api
you got everything there.
CodePudding user response:
blockchain.info has an API to get block details.
Using https://blockchain.info/block-height/$block_height
endpoint, you can pass $block_height
as block number that you need.
Then by simple script like below, you can extract the addresses.
<?php
$blockTxs = getBlockTxs(1);
foreach ($blockTxs as $tx) {
foreach ($tx->out as $out)
{
var_dump($out->addr);
}
}
function getBlockTxs($height) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://blockchain.info/block-height/' . $height,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
return (json_decode($response))->blocks[0]->tx;
}