我正在尝试使用PHP在我的网页中显示此API的结果。
我不知道是什么问题,但目前我的代码不起作用? (空白页)
<!DOCTYPE html>
<html>
<body>
<?php
$nethash = File_get_content("http://mattsmines.net/OMC/get.php?id=nethash");
die("{$nethash}");
?>
</body>
</html>
答案 0 :(得分:3)
您的File_get_content
功能错误应该是file_get_contents
见...
<!DOCTYPE html>
<html>
<body>
<?php
$nethash = file_get_contents("http://mattsmines.net/OMC/get.php?id=nethash");
die("{$nethash}");
?>
</body>
</html>
答案 1 :(得分:0)
echo $nethash = file_get_contents("http://mattsmines.net/OMC/get.php?id=nethash");
答案 2 :(得分:0)
因为您没有启用错误显示。如果是,那么您可以看到一条消息:
Fatal error: Call to undefined function File_get_content() in ...
使用此:
<!DOCTYPE html>
<?php
$nethash = File_get_contents("http://mattsmines.net/OMC/get.php?id=nethash");
die("{$nethash}");
?>
</body>
</html>
如果您正在开发任何应用程序(或在开发模式/环境中),您应该已启用显示错误(E_ALL
)。
答案 3 :(得分:0)
正确的(ish)答案是@ Mr.Smith的回答。但是我喜欢添加,使用die会停止输出并导致页面不打印其余的html。
此外,如果您的访问API被黑客攻击(可能)或发生故障(可能),您可以将所有用户暴露给它所服务的内容,从而查看您的页面。
curl比fgc更快,所以你可能会发现以下代码更有用。
<?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>
此外,如果你想从API中查询多个id,你可以将curl包装在一个函数中并传递id,(我还添加了一个简单的会话缓存,因此你不需要在每个页面加载时查询API,这将减慢请求并使您的页面变慢。)
<?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>
<?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>
此外,由于您希望多次调用API,因此您应该使用curl_multi一次执行所有请求,这将加快API请求。 Gist - curl_multi version In Action