Question

I'm trying to display the result of this API in my webpage with PHP.

I don't know what's the issue but currently my code is just not working? (Blank page)

<!DOCTYPE html>
<html>
<body>

<?php
$nethash = File_get_content("http://mattsmines.net/OMC/get.php?id=nethash");
die("{$nethash}");
?>

</body>
</html>
Was it helpful?

Solution 2

Correct(ish) answer is @Mr.Smith answer. but being me id like to add, that using die will stop the output and cause the page not to print the rest of the html.

Also that if the API your accessing got hacked(possible) or goes down(probable) you could expose all your users to what it serves, junking up your page.

And curl is faster over fgc, so perhaps you will find the following code more useful.

<?php 
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => 'http://mattsmines.net/OMC/get.php?id=nethash',
    CURLOPT_TIMEOUT => 5,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_FAILONERROR => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false,
));
$result = curl_exec($ch);

if(substr($result, -3) == 'h/s'){
    $result = 'Nethash is crunching @ '.htmlentities($result);
}else{
    $result = 'Error fething hash!';
}
?>
<!DOCTYPE html>
<html>
<body>
<p><?php echo $result; ?></p>
</body>
</html>

Also if you wanted to query multiple ids from the API you can wrap the curl in a function and just pass the id, (I also added a simple session caching so as you wont need to query the API on every page load, which will slow down the request and make your page slow.)

<?php 
session_start();

function get_hash($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,
        ));
        $result = curl_exec($ch);

        if(substr($result, -3) == 'h/s'){
            $_SESSION[md5($id)] = htmlentities(ucfirst($id)).' is crunching @ '.htmlentities($result);
        }else{
            $_SESSION[md5($id)] = 'Error fething hash for '.htmlentities($id);
        }
    }
    return $_SESSION[md5($id)];
}
?>
<!DOCTYPE html>
<html>
<body>
    <p><?php echo get_hash('nethash'); ?></p>
    <p><?php echo get_hash('some.other.id'); ?></p>
</body>
</html>

Edit (see comments) See it in action:

<?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)];
    }

    /**
     * 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];
    }

    /**
     * Display queried 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;
    }

    /**
     * Calculates 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';
    }

    /**
     * Query's 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 mining pool difficulty
     *
     * @return string
     */
    function total_diff(){
       $result = $this->call_api('diff');
       return 'The current Omnicoin difficulty is currently: '.number_format(htmlentities($result)).' .';
    }

}
/**
 * Example: 
 * 
 * Initialize the object and query the API, by passing the pool ids
 */
$omc = new omc();
$omc->get_pool_hash('nethash');
$omc->get_pool_hash('OMhash');
$omc->get_pool_hash('BLhash');
$omc->get_pool_hash('MOhash');
?>
<!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>

Also as you want to call the API multiple times you should utilize curl_multi to do all them requests at once, which will speed up the API request. Gist - curl_multi version In Action

OTHER TIPS

Your File_get_content function is wrong it should be file_get_contents See...

<!DOCTYPE html>
<html>
<body>

<?php
$nethash = file_get_contents("http://mattsmines.net/OMC/get.php?id=nethash");
die("{$nethash}");
?>

</body>
</html>
echo $nethash = file_get_contents("http://mattsmines.net/OMC/get.php?id=nethash");

Because you don't have enabled errors displaying. If yes, then you can see a message:

Fatal error: Call to undefined function File_get_content() in ...

Use this:

<!DOCTYPE html>

<?php
$nethash = File_get_contents("http://mattsmines.net/OMC/get.php?id=nethash");
die("{$nethash}");
?>

</body>
</html>

You should have enabled displaying errors (E_ALL) if you are developing any application (or in development mode/environment).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top