Skip to content

Instantly share code, notes, and snippets.

@lcherone
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/f5a3d4859c0d11cba868 to your computer and use it in GitHub Desktop.
Save lcherone/f5a3d4859c0d11cba868 to your computer and use it in GitHub Desktop.
http://mattsmines.net/ OMC pool API class (With curl_multi example)
<?php
session_start();
/**
* Simple OMC pool API class
*
* @author Lawrence Cherone
* @version 0.01
*/
class omc{
public $totalz = array();
/**
* Calls API and stores result in $_SESSION
*
* @param string $id
* @return string
*/
function call_api($id){
if(!isset($_SESSION[md5($id)])){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://mattsmines.net/OMC/get.php?id='.$id,
CURLOPT_TIMEOUT => 5,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
));
$_SESSION[md5($id)] = curl_exec($ch);
}
return $_SESSION[md5($id)];
}
/**
* Calls API using curl multi.
* Should be called if using $this->setpool()
*
* @param string $id
* @return string
*/
function query_multi() {
$curl = array();
$result = array();
$mh = curl_multi_init();
foreach ($this->pool as $id=>$url) {
$curl[$id] = curl_init();
curl_setopt($curl[$id], CURLOPT_URL, 'http://mattsmines.net/OMC/get.php?id='.$url);
curl_setopt($curl[$id], CURLOPT_HEADER, 0);
curl_setopt($curl[$id], CURLOPT_TIMEOUT, 30);
curl_setopt($curl[$id], CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; curl)');
curl_setopt($curl[$id], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $curl[$id]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($curl as $id=>$c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
foreach($result as $pool=>$hashrate){
$this->totalz[$pool] = (int) $hashrate;
}
}
/**
* Gets current pool hash speed and stores in $this->totalz scope.
*
* @param string $id
* @return int
*/
function get_pool_hash($id){
if(!isset($this->totalz[$id])){
$result = $this->call_api($id);
if(substr($result, -3) == 'h/s'){
$this->totalz[$id] = (int) $result;
}else{
$this->totalz[$id] = 0;
}
}
return $this->totalz[$id];
}
/**
* Sets pool id for curl multi.
*
* @param string $id
* @return int
*/
function set_pool($id){
if(!isset($this->pool[$id])){
$this->pool[$id] = $id;
}
return $this;
}
/**
* Display queryied pools as string
*
* @return string
*/
function display(){
$ret = null;
foreach($this->totalz as $pool=>$hashrate){
$ret .= ucfirst($pool).' is crunching @ '.number_format($hashrate).' Mh/s'.'<br>';
}
return $ret;
}
/**
* Cacluates the queried pools total hash rate and returns string
*
* @return string
*/
function total_rate(){
return 'Total Hash Rate: '.number_format(array_sum($this->totalz)).' Mh/s';
}
/**
* Querys the total OMC hash rate and returns string
*
* @return string
*/
function total_omc(){
$result = $this->call_api('totalomc');
return 'The total OMC volume is currently: '.number_format(htmlentities($result)).' Mh/s';
}
/**
* Get current minig pool dificalty
*
* @return string
*/
function total_diff(){
$result = $this->call_api('diff');
return 'The current Omnicoin difficulty is currently: '.number_format(htmlentities($result)).' .';
}
}
/**
* Example:
*
* Initilise the object and query the API, by passing the pool ids
*/
$omc = new omc();
$omc->set_pool('nethash')
->set_pool('OMhash')
->set_pool('BLhash')
->set_pool('MOhash')
->query_multi();
?>
<!DOCTYPE html>
<html>
<body>
<p><?php echo $omc->display(); ?></p>
<p><?php echo $omc->total_rate();?></p>
<p><?php echo $omc->total_omc();?></p>
<p><?php echo $omc->total_diff();?></p>
</body>
</html>
@lcherone
Copy link
Author

lcherone commented May 3, 2014

See it In Action on Viper7(codepad)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment