As indicated in our previous articles get payouts and get blocks here's another example of using the pool's API (v2).
You can get the pool's most recent 10 minute average BCH hashrate (or BTC or BCH & BTC combined) by including 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
$timestarts = (new DateTime('-15 minutes'))->format('c');
$timeends = (new DateTime(''))->format('c');
//Date & time must be in ISO 8601 format. Set the start time at minus 15 minutes and leave the end time blank. This will return the most recent 10 minute average. Using less than minus 15 will sometimes not return hashrate and using much more than minus 15 will begin to return the previous 10 minute average(s).
$settimeandcoin = (array("start" => $timestarts, "end" => $timeends, "coin" => "BCH"));
Set coin as BCH and add the variables for the start and end times.
$settimeandcoinrequestinjson = json_encode($settimeandcoin);
//Why? Because the request must be made in json. Could also use this by itself: $settimeandcoin = json_encode(array("start" => $timestarts, "end" => $timeends, "coin" => "BCH"));
$apiurl = "https://console.pool.bitcoin.com/api/v2/pool/hashrates";
//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.
$context_options = array('http' =>
array('method' => "GET",
'header' => "Content-Type: application/json; charset=UTF-8\n" . "$apikeyforheader",
'content' => "$settimeandcoinrequestinjson"
)
);
/*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' => "$settimeandcoinrequestinjson"
]
];*/
$context = stream_context_create($context_options);
$together = file_get_contents($apiurl, false, $context);
$info = json_decode($together,true);
$rateplain = $info['coins']['BCH']['hashRates'][0];
function number_abbr($decimalunitprefix) {
$abbreviations = [24 => ' YH/s', 21 => ' ZH/s', 18 => ' EH/s', 15 => ' PH/s', 12 => ' TH/s', 9 => ' GH/s', 6 => ' MH/s', 3 => ' KH/s', 0 => ''];
/*Abrrevations for: Yotta, Zetta, Exa, Peta, Tera, Giga, Mega and Kilo. For the time being the pool's hashrate will likely not fall below or rise above Petahashes. During the November 2018 hash war (ABC vs BSV) the pool's hashrate reached an all time high of approximately 4 Exahashes (or 4000 Petahashes).
1 KH/s is 1,000 hashes per second.
1 MH/s is 1,000,000 hashes per second.
1 GH/s is 1,000,000,000 hashes per second.
1 TH/s is 1,000,000,000,000 hashes per second.
1 PH/s is 1,000,000,000,000,000 hashes per second.
1 EH/s is 1,000,000,000,000,000,000 hashes per second.
1 ZH/s is 1,000,000,000,000,000,000,000 hashes per second.
1 YH/s is 1,000,000,000,000,000,000,000,000 hashes per second.
*/
foreach ($abbreviations as $exponent => $abbr) {
if (abs($decimalunitprefix) >= pow(10, $exponent)) {
$display = $decimalunitprefix / pow(10, $exponent);
$decimals = ($exponent >= 3 && round($display) < 100) ? 3 : 3;
//Why? To round to and display 3 decimal places.
$decimalunitprefix = number_format($display, $decimals).$abbr;
break;
}
}
return $decimalunitprefix;
}
$poolhashratebch = number_abbr($rateplain);
echo "Bitcoin.com Mining Pool most recent 10 minute average BCH hashrate: " . $poolhashratebch;
//Including the following just to show the hashrate returned without rounding & prefix:
echo "Without decimal unit prefix & rounding: " . $rateplain;
?>
Results (pool's BCH hashrate) available at nandibear.com/bitcoin-com-mining-pool/api-v2-get-pool-hashrate/
Article word count: 79
Count including updates (if any): 79
Thanks for stopping by and reading!
PLEASE leave some comments! Talk a little!
↓ --- You may also find the Nandi Bear at --- ↓