Bitcoin.com Mining Pool API v2 - Get Blocks Using Pure PHP

May 6, 2020 @ 4:37 pm - Mississippi and Louisiana, USA | May 6, 2020 @ 6:37 pm - Greater Antilles, CAR 
May 6, 2020 @ 11:37 pm - Rome, ITA | May 7, 2020 @ 12:37 am - Athens, GRC | May 7, 2020 @ 7:37 am - Tokyo, JPN 

by Luke Nandibear  
 
nandibear.com/img/luke-nandibear-bitcoin-com-mining-pool-api-v2-get-blocks-pure-php

PHP (without cURL) to get BCH blocks mined by the Bitcoin.com Mining Pool

Will try to share a few more of these this week (examples: get pool's BCH hashrate, get your BCH hashrate, get your payouts).

You can either include the following code on the page you want the results to display on or you can save this as a file itself and call it on any page.

<?php

$setcoin = (array("limit" => "100", "coin" => "BCH"));

//Set the limit to any number of last blocks mined by the pool. Set coin to BCH or BTC.

$setcoinrequestinjson = json_encode($setcoin);

//Why? Because the request must be made in json. Could also use this by itself: $setcoin = json_encode(array("limit" => "100", "coin" => "BCH"));

$apiurl = "https://console.pool.bitcoin.com/api/v2/pool/blocks";

//URL from https://console.pool.bitcoin.com/apidoc/

$apikey = "POOL-BITCOIN-COM-API-KEY:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; 

//Generate API key at https://console.pool.bitcoin.com/settings and replace the x's above with your key.

$apikeyforheader = $apikey;

//Why? It's not necessary but might hide key if dropped in some scenarios.

$context_options = array('http' =>
    array('method'  => "GET",
          'header'  =>  "Content-Type: application/json; charset=UTF-8\n" . "$apikeyforheader",
          'content' => "$setcoinrequestinjson"
    )
);

/*If you don't want to array use brackets:
$context_options = [
    'http' => [
            'method' => "GET",
            'header' => "Content-Type: application/json; charset=UTF-8\n" . "$apikeyforheader",
            'content' => "$setcoinrequestinjson"
     ]
        ];*/
        
$context  = stream_context_create($context_options);
$together = file_get_contents($apiurl, false, $context);
$info['blocks'] = json_decode($together,true);

foreach($info as $itemmaker) {
   
    echo "Block: " . "<a href='https://explorer.bitcoin.com/bch/block/" . $itemmaker['blocks'][0]['blockHeight'] . "'>" . $itemmaker['blocks'][0]['blockHeight'] . "</a>";
    $timeblockfound = round($itemmaker['blocks'][0]['unixTime']/1000); echo "Date/Time: " . date('M-d-Y H:i:s', $timeblockfound);
    
//Why? Because the time is in milliseconds. You can divide by 1000 by using the above.

    echo "Block reward (includes transaction fees): " . $itemmaker['blocks'][0]['reward'] . " BCH";
    echo "Block hash (Little Endian): " . $itemmaker['blocks'][0]['blockHash'];
    
     function littleToBigEndian($little) {
  return implode('',array_reverse(str_split($little,8)));}
  
//Why? Because, for example, block explorers don't use little endian. Convert to big endian using the above function.

    echo "Block hash (Big Endian): " . littleToBigEndian($itemmaker['blocks'][0]['blockHash']);

//The foregoing will return the block & related data most recently found by the Bitcoin.com Mining Pool.  To display more blocks & related data copy and paste the code within the foreach construct and change the number [0] in the brackets to number [1] and so on. Continuing below with [1].

    echo "Block: " . "<a href='https://explorer.bitcoin.com/bch/block/" . $itemmaker['blocks'][1]['blockHeight'] . "'>" . $itemmaker['blocks'][1]['blockHeight'] . "</a>";
    $timeblockfound = round($itemmaker['blocks'][1]['unixTime']/1000); echo "Date/Time: " . date('M-d-Y H:i:s', $timeblockfound);
    echo "Block reward (includes transaction fees): " . $itemmaker['blocks'][1]['reward'] . " BCH";
    echo "Block hash (Little Endian): " . $itemmaker['blocks'][1]['blockHash'];
    echo "Block hash (Big Endian): " . littleToBigEndian($itemmaker['blocks'][1]['blockHash']);
}

?>

Results (ten most recent blocks found by the pool) available at nandibear.com/bitcoin-com-mining-pool/api-v2-get-blocks/

Article word count: 69

Count including updates (if any): 69

nandibear.com nandi bear luke

Thanks for stopping by and reading!

PLEASE leave some comments! Talk a little!

↓ --- You may also find the Nandi Bear at --- ↓

Bitcoin.com Forum

Hive.blog/@nandibear

BitcoinCashColoredCoins.com